1

I have an index file (called runnumber_odour.txt) that looks like this:

run00001.txt   ptol
run00002.txt   cdeg
run00003.txt   adef
run00004.txt   adfg

I need some way of loading this in to a matrix in matlab, such that I can search through the second column to find one of those strings, load the corresponding file and do some data analysis with it. (i.e. if I search for "ptol", it should load run00001.txt and analyse the data in that file).

I've tried this:

clear; clc ;
% load index file - runnumber_odour.txt
runnumber_odour = fopen('Runnumber_odour.txt','r');
count = 1;
lines2skip = 0;

while ~feof(runnumber_odour)

 runnumber_odourmat = zeros(817,2);
 if count <= lines2skip
     count = count+1;
     [~] = fgets(runnumber_odour); % throw away unwanted line
     continue;
 else
     line = strcat(fgets(runnumber_odour));
     runnumber_odourmat = [runnumber_odourmat ;cell2mat(textscan(line, '%f')).'];
     count = count +1;
   end
end

runnumber_odourmat

But that just produces a 817 by 2 matrix of zeros (i.e. not writing to the matrix), but without the line runnumber_odourmat = zeros(817,2); I get the error "undefined function or variable 'runnumber_odourmat'.

I have also tried this with strtrim instead of strcat but that also doesn't work, with the same problem.

So, how do I load that file in to a matrix in matlab?

1 Answer 1

2

You can do all of this pretty easily using a Map object so you will not have to do any searching or anything like that. Your second column will be a key to the first column. The code will be as follows

clc; close all; clear all;
fid = fopen('fileList.txt','r'); %# open file for reading
count = 1;
content = {};
lines2skip = 0;
fileMap = containers.Map();
while ~feof(fid)
    if count <= lines2skip
        count = count+1;
        [~] = fgets(fid); % throw away unwanted line
    else
        line = strtrim(fgets(fid));
        parts = regexp(line,' ','split');
        if numel(parts) >= 2
            fileMap(parts{2}) = parts{1};
        end
        count = count +1;
    end
end
fclose(fid);

fileName = fileMap('ptol')

% do what you need to do with this filename

This will provide for quick access to any element

You can then do what was described in the previous question you had asked, with the answer I provided.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for that, it seems to work apart from the last line which gives this error: Error using containers.Map/subsref The specified key is not present in this container. Error in filemaptest (line 28) fileName = fileMap('ptol')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.