I am not sure about the forum etiquette. But I am giving an update in case anyone encounters the same issue. I just "saved as" the function with a new name, and it worked fine. I literally did not change a single line of code. I am out of the problem now, but if anyone has any idea what could be the problem pleaseee let me know. I am flabbergasted.
Error using cell2mat
4 次查看(过去 30 天)
显示 更早的评论
I am using the same code I have used several times successfully, but now i get an error using cell2mat. The code is below, the function actually continues (thus the other variables), but I am getting the error:
Error using cat
Dimensions of arrays being concatenated are not consistent.
Error in cell2mat (line 75)
m{n} = cat(2,c{n,:});
If I use THE SAME code outside the function (copy the code and define the variables in the cose), using the same n as cell array, I have no problems. It is driving me crazy. I can run the function in other codes as well. I am using version 2022b of MATLAB.
Any ideas?
Thank you
Edit: I have the close all, clear, clc in the code trying to run the function. The test with the code outside the function was performed on the same script.
function [Rs,Ts,Rp,Tp,Msi,Mpi]=TMM_fresnel_inc_file(n,e,phi0,lambda,coh)%n es cell array
%Ccoh es 0 si coherente, 1 si incoherente
num_index=zeros(1,length(n));
%----
for k=1:length(n)
num_index(k)=isnumeric(n{k});
end
%---
if any(num_index==0)
index_string=find(num_index==0);%
for k=1:length(index_string)
data=readmatrix(n{index_string(k)});
n(index_string(k))={data(data(:,1)==lambda,2)-1i*data(data(:,1)==lambda,3)};
end
end
n=cell2mat(n); %Ya no tengo mas string, tranformo a n en un vector como antes
end
2 个评论
dpb
2023-7-12
编辑:dpb
2023-7-12
That would all be data-dependent upon what is in the content of n which is not given; the function is brittle in using length; it would return row size or column size depending upon the shape of the cell array, independent of the content within the individual cells.
Then, whatever it is that readmatrix returns is weird coding -- using the numeric value from find as an argument there seems more than a little strange.
Only way to say anything would be to have actual data that illustrates the problem; one can only presume the case(s) working aren't actually the same in some critical detail as those that didn't.
Of course, there's always the possibility that in the script you inadvertently aliased something that broke it as well and changing the name cleared that condition. Simply not enough context to be able to conclude anything, sorry.
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!