Creating an array out of a for loop.

Using a for loop to create an array of file information however when I run the code I get an array of the correct size but only one cell had data in it...
struct = cell(length(fileID), 3);
for j = 1:length(fileID)
fID = char(fileID(j));
if exist(fID, 'file');
info = dicominfo(fID);
serDes = info.SeriesDescription;
instNo = info.InstanceNumber;
sliceLoc = info.SliceLocation;
L = length(fID);
fName = fID(L-2:L);
struct{k} = [fName, serDes, instNo];
else
end
end
Struct
Any help would be great!

 Réponse acceptée

Guillaume
Guillaume le 4 Fév 2015
A few things of note:
  • struct is a very common matlab function, don't use this as a variable name regardless of its content.
  • cell arrays and structure are two different container, so don't name a cell array struct or structure or anything that hints at structure. It's just plain confusing to anyone reading your code.
  • Learn to use the end keyword. These two lines:
L = length(fID);
fName = fID(L-2:L);
are equivalent to:
fName = fID(end-2:end);
With that said, the cause of your problem is pretty simple. You're using k as an index to store values in your cell array, whereas your loop index is j. k never changes within your loop.

3 commentaires

Shaun
Shaun le 4 Fév 2015
Modifié(e) : Shaun le 4 Fév 2015
Thanks for your quick response. That was a stupid mistake.... Thanks for the other hints as well! After editing my code I get an array which has two empty columns in each row and the first column has all of the information from the loop...
'Z22Calibration Scan/' [] []
'Z23Calibration Scan2' [] []
'Z24Calibration Scan3' [] []
'Z25Calibration Scan6' [] []
How do I fix this so that that each set of information is in its own column? e.g...
'Z25' 'Calibration Scan' [Number]
Assuming you've called your cell array mycell:
mycell(j, :) = {fName, serDes, instNo};
Shaun
Shaun le 4 Fév 2015
Thank you very much!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Design and Simulate SerDes Systems dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by