MATLAB Answers

0

Insert blank/NaN rows in a column

Mahrukh Farooq さんによって質問されました 2019 年 5 月 21 日
最新アクティビティ Mahrukh Farooq さんによって コメントされました 2019 年 5 月 22 日
I actually want to plot yearly graphs 12 curves for 12 months each with a set of 24 values. I have acess the data from excel files and reshaped them in one cloumn 288 rows in total. What I want is gap between is each month with no connecting points between the curves. My solution is to introduce blank rows after 24th in each month. How can i do that?

  0 件のコメント

サインイン to comment.

3 件の回答

Rik
回答者: Rik
2019 年 5 月 21 日
編集済み: Rik
2019 年 5 月 21 日
 採用された回答

This example should work. Don't forget to add NaN values to your x-parameter as well.
Alternatively, you could plot them as separate line objects so you don't have to mess around with NaN values.
data=(1:12*24)';
x_vals=reshape(repmat((1:24)',1,12),[],1);
%strategy 1: insert NaNs
y1=reshape(data,24,[]);
y1(end+1,:)=NaN;
y1=y1(:);
x1=reshape(x_vals,24,[]);
x1(end+1,:)=NaN;
x1=x1(:);
%strategy 2: plot separately
y2=reshape(data,24,[]);
x2=reshape(x_vals,24,[]);
%show plots
figure(1),clf(1)%only use clf for debugging
subplot(1,2,1)
plot(x1,y1)
xlim([0 35])
legend%show legend to show effect
subplot(1,2,2)
plot(x2,y2)
xlim([0 35])
legend%show legend to show effect

  3 件のコメント

Mahrukh Farooq 2019 年 5 月 21 日
By separate line objects you mean? I mean how?
Rik
2019 年 5 月 21 日
You can use the strategy like Dheeraj described. Providing a matrix as input to plot is equivalent to looping over the columns of your matrix and calling plot repeatedly (with hold on).
I'll edit my answer to illustrate the two methods. You can then make the choice you prefer.
Mahrukh Farooq 2019 年 5 月 22 日
Thanks a lot what i needed was a graph like this below and your first method worked. Actually i have not been using matlab for a year or two and have nearly forgotten everything even how small codes (like the one you gave) work :D I wanted some small changes in my older codes. So your idea to use Dheeraj's method by looping is a bit lengthy and complicated for me right now. Ill be needing help in future
Capture.PNG
Thanks again.

サインイン to comment.


Alex Mcaulley
回答者: Alex Mcaulley
2019 年 5 月 21 日

Do you mean this?
A = rand(288,1);
A = reshape(A,[24,12])
B = reshape(1:numel(A),[24,12])
plot(B,A)

  0 件のコメント

サインイン to comment.


回答者: Dheeraj Singh 2019 年 5 月 21 日

Hi,
You can reshape the matrix into a 24x12 matrix and directly plot
%suppose this is your data
val=rand(24*12,1)
%reshape this to 24x12
val=reshape(val,24,12);
plot(val);
plot_for_12.png
I got the result for 12 months .....Since the plot seems very clutterred for 12 months, i'll show you the plot for 3 months instead of 12...
I got the following result for 3 months....
plot_for_3.png
I hope this helps!!!

  1 件のコメント

Mahrukh Farooq 2019 年 5 月 22 日
Thanks Dheeraj but i needed a continuous graph not overlapping like the one i have shared above. Thanks for your efforts

サインイン to comment.



Translated by