Error finding and graphing max values of data (Error using horzcat Dimensions of matrices being concatenated are not consistent. )

I'm trying to graph multiple sets of .csv data and display the max value for each data set on the graph itself. This has worked for all of my data sets except for one, which I keep getting the error "Error using horzcat Dimensions of matrices being concatenated are not consistent. The following is the code I've been using for two data sets, which I have attached to the post. TEK00055 is the one with errors and TEK00057 is the one without issues.
figure;
tmd=load('TEK00055.CSV');
x=5.+tmd(:,1);
y=5.*tmd(:,2);
plot(x,y)
xlabel('Time (s)')
ylabel('disp (mm)')
indexmax = find(max(y) == y);
xmax = x(indexmax);
ymax = y(indexmax);
strmax = ['Maximum = ',num2str(ymax)];
text(xmax,ymax,strmax,'HorizontalAlignment','right');
figure;
tmd=load('TEK00057.CSV');
x=5.+tmd(:,1);
y=5.*tmd(:,2);
plot(x,y)
xlabel('Time (s)')
ylabel('disp (mm)')
indexmax = find(max(y) == y);
xmax = x(indexmax);
ymax = y(indexmax);
strmax = ['Maximum = ',num2str(ymax)];
text(xmax,ymax,strmax,'HorizontalAlignment','right');
For the TEK00057 data, a graph comes out exactly how I want, with the maximum y-value being labelled, but the TEK00055 data is giving me errors. From looking through the excel files, i can't find any major differences between the two. Any help would be appreciated.

6 Comments

Zaman - which line of code is throwing the error? Is it
strmax = ['Maximum = ',num2str(ymax)];
If it is, what is ymax? Is it a scalar or an array of more than one element?
The program doesn't say where the error is, it just gives me the message "Error using horzcat Dimensions of matrices being concatenated are not consistent." This makes me think it's something going on in the data file, since both sets of code are identical except for the data files they call.
usually the full error message gives the line number of where the code is throwing the exception. If you put a breakpoint at the first line of your code and then run it, you should be able to step over each line until the exception is thrown. Which line is that?
It starts giving me issues at the
strmax = ['Maximum = ',num2str(ymax)];
portion of the code. The reason I'm so confused is because both sets of data are 10000x2 matrices, and both sets of code are identical. It's just that when the code calls the TEK00055 data, it gives me the error.
Zaman - but what are the dimensions of ymax? Perhaps there are two or more idenitcal maximum values. And so the horizontal concatenation will fail when trying to create this string. If you are unsure, then put in some code like
strmax = ['Maximum = ',num2str(ymax(1))];
so that you always grab the first element of ymax.
You were correct. I had to designate xmax=xmax(1) and ymax=ymax(1), so that there would only be one of each values in the text line. Someone posted an answer, and I gave that credit, but thank you for helping me with my issue.

Sign in to comment.

 Accepted Answer

Your TEK00055.csv data has three data points with maximum y-values:
>> xmax
xmax =
6.8720
6.8730
6.9890
>> ymax
ymax =
0.2380
0.2380
0.2380
And then
>> num2str(ymax)
ans =
3×5 char array
'0.238'
'0.238'
'0.238'
is a 3x1 array, which cannot be horizontally concatenated with a single string 'Maximum = '.
You can use ['Maximum = ', str2num(ymax(1))] to display the first maximum occurrence, or any other solution.

1 Comment

What you and Geoff Hayes said about designating ymax(1) cause I had multiple values in ymax that were the same was correct. What I ended up doing to get a reasonable result is
figure;
tmd=load('TEK00055.CSV');
x=5.+tmd(:,1);
y=5.*tmd(:,2);
plot(x,y)
% xlim([0,10])
xlabel('Time (s)')
ylabel('disp (mm)')
indexmax = find(max(y) == y);
xmax = x(indexmax);
ymax = y(indexmax);
xmax = xmax(1);
ymax = ymax(1);
strmax = ['Maximum = ',num2str(ymax)];
text(xmax,ymax,strmax,'HorizontalAlignment','right');
I had to designate the xmax=xmax(1) and ymax=ymax(1), so that there was only one of each value to be noted in the text line.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!