Question

I have a 'text' file having content as follows:

012abc3cb7503bddef3ff59e0e52fd79.jpg 160.073318 18.588472 14.246923 8.054444 6.600504 6.261390 5.838249 4.447019 3.639888 3.357715 2.996645 2.910991 2.574769 2.527163 2.343448 2.264113 2.176161 2.088773 1.915582 1.902159 1.836033 1.725432 1.667595 1.633245 1.557424 1.542059 1.434280 1.430181 1.321047 1.302652 1.272890 1.261313 1.188892 1.138115 1.114376 1.070352 1.044311 1.025954 0.993622 0.988920 0.969866 0.933977 0.931669 0.913624 0.882856 0.876036 0.840088 0.822686 0.814072 0.787075 0.781157 0.778171 0.763771 0.748851 0.740975 0.708208 0.691589 0.688566 0.664124 0.659779 0.644820 0.623200 0.614799 0.607180 0.590615 0.578751 0.57...........

Each row represents an image (instance) with the first column being image name, remaining 240 columns being the feature-vector of the image.

How can I load this file in MATLAB and store the image names into 'names' variable and the 240 values into a 'histogram' variable?

Was it helpful?

Solution 2

str = textread('tmp.txt','%s');
str = reshape(str,241,[]).';
names = str(:,1); %'// cell array of strings with image names
histogram = str2double(str(:,2:end)); %// each row refers to an image

OTHER TIPS

You might be able to do it with textscan and repmat so you can avoid conversion from strings:

Nfeatures = 240;
fid = fopen('text.txt');
format = ['%s ' repmat('%f', [1 Nfeatures])];
imageFeatureCell = textscan(fid, format, 'CollectOutput', true);
fclose(fid);

A test on a file with 7 rows:

>> fileData
fileData = 
    {7x1 cell}    [7x240 double]

Move into your desired variables:

names = fileData{1};      % names{1} contains first file name, etc.
histogram = fileData{2};  % histogram(1,:) contains first file's features
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top