How to plot from an array

I'm reading measurement data from my arduino with matlab using Serial communication, that part seems to work but I am now stuck on creating a matrix of those values and making a plot from that matrix. So I tried just adding the values every itteration of the loop but I cannot figure out how to do so.
for ii = 0:Tp:10
fprintf(s,'Z') % Sends measurement command to arduino
A=str2num(fscanf(s)); % Reads measurement from arduino
pause(Tp) % Pause based on previously set time
Time= A(1,1); % Returns only Timestamp (arduino)
Temp= A(1,2); % Returns only Temperature
RH= A(1,3); % Returns only Relative Humidity
end
I thought of adding
plot(Time,Temp) hold on
at the end of every iteration of the loop but that just ends up being a blank figure with the right corresponding numbers on the axes but no data markers

3 件のコメント

KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 4 月 17 日
編集済み: KALYAN ACHARJYA 2019 年 4 月 17 日
% Not sure about your code
% Here I am trying to create vector of Time and Temp with equal length,
% Assuming tp value is 1
for ii = 0:Tp:10
fprintf(s,'Z') % Sends measurement command to arduino
A=str2num(fscanf(s)); % Reads measurement from arduino
pause(Tp) % Pause based on previously set time
Time(ii)=A(1,1); % Returns only Timestamp (arduino)
Temp(ii)=A(1,2); % Returns only Temperature
RH=A(1,3); % Returns only Relative Humidity
end
plot(Time,Temp)
Wouter Donkers
Wouter Donkers 2019 年 4 月 17 日
Thnx for the answer, after applying your code I realised what I was doing wrong. I had the start of ii set to zero so I couldn't use
Time(ii)=A(1,1);
since it would give an error. I assumed this was because I wasn't using the ii as a part of the function which I now realize is a wrong assumption. Code is working now, just had to replace
for ii = 0:Tp:10
with
for ii = 1:Tp:10
and it worked.
KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 4 月 17 日
編集済み: KALYAN ACHARJYA 2019 年 4 月 17 日
Great!

サインインしてコメントする。

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMATLAB についてさらに検索

製品

リリース

R2019a

質問済み:

2019 年 4 月 17 日

編集済み:

2019 年 4 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by