Unclear as to why I'm getting a dimension mismatch?
Show older comments
This is the bit of code that is giving me trouble:
>> strcat('spectra',num2str(j)) = zeros(1340,4);
??? Subscripted assignment dimension mismatch.
Only thing I can think of is that matlab believes I'm attempting to assign a matrix into a string. Where as what I'm trying to do is assign the string as the variable for a matrix. For reference j is just an integer counter.
edit Basically trying to create an increasing variable creation so that for each loop of the for fxn I get a new matrix with the assigned variable that then has memory preallocated.
edit2
eval(['spectra' int2str(j) '=zeros(1340,4);'])
Is what I finally settled on... the full loop:
for j=1:size(filelist,1)
eval(['spectra' int2str(j) '=zeros(1340,4);'])
eval(['spectra' int2str(j) '(1:1340,3:4)=xlsread(filelist(j,1:end));'])
for k=1:1340
eval(['spectra' int2str(j) '(k,1)= k*correction(1,1)+correction(1,2);'])
eval(['spectra' int2str(j) '(k,2)=(10^7)/532-spectra' int2str(j) '(k,1);'])
eval(['spectra' int2str(j) '(k,3)=1/spectra' int2str(j) '(k,2)*(10^7);'])
end
end
I've seen some mentions of increased speed doing other methods. While this isn't really a time intensive script, I wouldn't mind speeding it up a bit so its a bit more responsive when I'm working.
Thanks for the responses all!
Accepted Answer
More Answers (3)
Walter Roberson
on 13 Jul 2011
spectra{j} = zeros(1340,4);
5 Comments
Karl
on 13 Jul 2011
Karl
on 13 Jul 2011
Daniel Shub
on 13 Jul 2011
The "{}" create cell arrays.
Karl
on 13 Jul 2011
Walter Roberson
on 13 Jul 2011
for j=1:size(filelist,1)
spectra{j}=zeros(1340,4);
spectra{j}(1:1340,3:4)=xlsread(filelist(j,1:end));
and so on.
the cyclist
on 14 Jul 2011
I believe this is the correct transliteration of your code into cell array syntax. I have to admit I have not tried executing it, though. Too lazy to figure out the Excel file, etc.
lengthFileList = size(filelist,1);
spectra = cell(lengthFileList,1);
for j=1:lengthFileList
spectra{j}=zeros(1340,4);
spectra{j}(1:1340,3:4)=xlsread(filelist(j,1:end));
for k=1:1340
spectra{j}(k,1)= k*correction(1,1)+correction(1,2);
spectra{j}(k,2)=(10^7)/532-spectra1(k,1);
spectra{j}(k,3)=1/spectra1(k,2)*(10^7);
end
end
Daniel Shub
on 13 Jul 2011
First, Walter is correct that you likely don't want to do what you are trying to do. Second, I don't think you are doing what you think you are doing. Third, I am a little surprised by the error you get. I believe what you are really doing is equivalent to:
clear x;
x(double('spectra'),double(num2str(1))) = zeros(1340, 4);
strcat = x;
since
double('spectra')
equals
ans =
115 112 101 99 116 114 97
there is a dimension mismatch.
Categories
Find more on Introduction to Installation and Licensing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!