Is This Concatenation Error a Bug?

5 Ansichten (letzte 30 Tage)
Dan Vergara
Dan Vergara am 1 Okt. 2020
Kommentiert: Walter Roberson am 12 Okt. 2020
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 Kommentare
Dan Vergara
Dan Vergara am 12 Okt. 2020
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 am 12 Okt. 2020
@Dan Vergara: please upload an MWE so that we can test it. Also tell us your MATLAB version.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Steven Lord
Steven Lord am 1 Okt. 2020
(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 Kommentare
Dan Vergara
Dan Vergara am 12 Okt. 2020
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 am 12 Okt. 2020
(Me, picturing Steven saying "Trust by verify!" in a fake Russian accent...)

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Performance and Memory finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by