Store multiple variables for loop

12 次查看(过去 30 天)
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.

采纳的回答

Rik
Rik 2017-8-14
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

更多回答(1 个)

Stephen23
Stephen23 2017-8-14
编辑:Stephen23 2017-8-14
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:

类别

Help CenterFile Exchange 中查找有关 Scripts 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by