Is This Concatenation Error a Bug?

I have a block of code that processes two vectors t and x that are Nx1 double data types (t and x come from a .mat file and are always equal dimensions and data types). In the routine, I'm trying to concatenate these vectors to create an Nx2 array. I had this block of code working for months in a script, and now that I'm moving it into a function, I'm getting the "dimensions of these arrays are not consistent" error when I try to concatenate!
function [] = YAHBOI(araw,b)
indb = find(b);
yah = araw(indb);
Nyah = length(yah);
boi = {};
for i = 1 : Nyah
boii = yah{i};
file = [boii,'.mat'];
load(file);
boi_rawi = [t, x];
boi = [boi, boi_rawi];
end
end
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
Error in YAHBOI (line 14)
boi_rawi = [t, x];
I'm not new to MATLAB... I know that this should work in theory. In fact, I tried a few workarounds when stepping through the function:
  1. When I got to the line of interest, I copied and pasted it into the console to see if it would throw an error. It worked, so that's kind of interesting but still expected.
  2. I tried 1) a second time, but this time I made a quick change to the code:
...
load(file);
y = x;
boi_rawi = [t, y];
...
This change didn't work either.
This seems like a bug to me. Is it? Or am I making some fundamental misunderstanding of how the enviroment works?
Thanks for any assistance!

댓글 수: 3

It is possible that one (or more) of those files does not contain the presumed variables, in which case your code will simply use the variable from a previous loop iteration. In the best case this will throw an error, but in the worst case your code will simply continue using incorrect data without any warning. Basically loading directly into the workspace like that is not robust. The robust approach is to always load into an output variable (which is a scalar structure) and access its fields:
boi = cell(1,Nyah);
for k = 1:Nyah
F = sprintf('%s.mat',yah{k});
S = load(file);
boi{k} = [S.t,S.x];
end
I made a few other improvements as well, such as preallocating the cell array before the loop, and using sprintf.
Dan Vergara
Dan Vergara 2020년 10월 12일
But my question is “shouldn’t this work anyway?” I know that the variables loaded properly, because when I step up to that line, I can go into the console and execute that line manually. It works with no errors in the console, and it even works when the entire code is run in a script. However, it doesn’t work in a function and I know that it isn’t because of the data contents. I understand that I can use a different method to make my code work, and thanks for showing me that, but isn’t it a strange issue? I can’t identify the cause...
Stephen23
Stephen23 2020년 10월 12일
@Dan Vergara: please upload an MWE so that we can test it. Also tell us your MATLAB version.

댓글을 달려면 로그인하십시오.

답변 (1개)

Steven Lord
Steven Lord 2020년 10월 1일

1 개 추천

(t and x come from a .mat file and are always equal dimensions and data types).
Trust but verify. Set an error breakpoint and run your code. When MATLAB stops on that line, verify that what you trust is true actually is.

댓글 수: 2

Dan Vergara
Dan Vergara 2020년 10월 12일
I did verify in the console that the dimensions were equal. I found that I could actually get past this line when I stepped up to it and then executed it in the console. The same exact code also works outside of a function.
Walter Roberson
Walter Roberson 2020년 10월 12일
(Me, picturing Steven saying "Trust by verify!" in a fake Russian accent...)

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Performance and Memory에 대해 자세히 알아보기

질문:

2020년 10월 1일

댓글:

2020년 10월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by