Store multiple variables for loop

13 views (last 30 days)
Ihiertje
Ihiertje on 14 Aug 2017
Answered: Rik on 14 Aug 2017
Hi,
I researched the forum to find an answer to my question, but although there are many topics about this I could not find the optimal solution for my problem yet. So hopefully you can help.
I have an ini.file which contains information for each image (thefiles.ini(k).name). I want to store the name for the image and the three variabels (rgb_exp, fluo_exp, excit_exp) in a array for all 42 images. However, I have not succeeded yet. First, when extracting the baseFileName, he sees it as the total image instead of just the name. Second, I cannot find a right way to save the three variables resulting the following table:
ExposureTimes = [];
for k = 1:length(theFiles_excit)
baseFileName_ini = theFiles_ini(k).name;
fullFileName_ini = fullfile(myfolder, baseFileName_ini);
[readsett,result] = inifile(fullFileName_ini,'readall');
rgb_exp = char(readsett(1,4)); %exposure time RGB
fluo_exp = char(readsett(4,4)); % exposure time fluorescence
excit_exp = char(readsett(5,4)); % exposure time excitation
%ExposureTimes(k,:,:,:)=[baseFileName_ini, rgb_exp]
k = k+1
end
Thank in advance.

Accepted Answer

Rik
Rik on 14 Aug 2017
There are a few things about your code that are not usually done.
  • if you know what size a matrix will be, pre-allocate it
  • what you are trying here is to write to a 4-dimensional matrix for which Matlab doesn't know the dimensions yet in the first loop. You're writing a name to that matrix, which is a string of what could be varying lengths. If you want to mix different data types, try a cell.
  • you don't need k = k+1 in a for-loop. In a while-loop you would need this (and you forgot the semicolon)
  • you convert some output to char, but this is usually not what people think it means. If you want to convert a numeric value to a string, use num2str, sprintf (or fprintf).
I don't see any reason why baseFileName_ini would be anything other than a file name (which is saved a string/char vector). I would suggest some edits to this code:
ExposureTimes = cell(length(theFiles_excit),4);
for k = 1:length(theFiles_excit)
baseFileName_ini = theFiles_ini(k).name;
fullFileName_ini = fullfile(myfolder, baseFileName_ini);
[readsett,result] = inifile(fullFileName_ini,'readall');
ExposureTimes{k,1} = baseFileName_ini;
ExposureTimes{k,2} = readsett(1,4); %exposure time RGB
ExposureTimes{k,3} = readsett(4,4); % exposure time fluorescence
ExposureTimes{k,4} = readsett(5,4); % exposure time excitation
end

More Answers (1)

Stephen23
Stephen23 on 14 Aug 2017
Edited: Stephen23 on 14 Aug 2017
The basic problem is trying to put them all data into the same array. While it would be possible to put strings (1xN char) into a larger char matrix, in practice this requires all of them to have the same length, or using some fancy indexing. Assuming that filenames have the same length is fragile, and likely to break. Fancy indexing is usually a waste of time. Much simpler would be to put your data cell arrays, where their length is not important. Once in the cell array you can easily check them, process them, or check their lengths and if they are all the same convert to a char array (should this really be required). To be honest, there are very few times that you are likely to need a char array, so why not simply use a cell array to hold those strings?
Something like this will do the job (untested):
N = numel(theFiles_ini);
baseNames = cell(N,1);
expImport = cell(N,3);
for k = 1:N
baseFileName_ini = theFiles_ini(k).name;
fullFileName_ini = fullfile(myfolder, baseFileName_ini);
[readsett,result] = inifile(fullFileName_ini,'readall');
baseNames{k,1} = baseFileName_ini;
expImport(k,:) = readsett([1,4,5]); % or maybe readsett([1,4,5],4);
end
Note that you do not need to increment loop iterator k yourself: the for loop does that for you. Also note how I preallocated the output cell arrays:

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!