Date strings with vector results

조회 수: 8 (최근 30일)
Manuel Flores
Manuel Flores 2018년 8월 5일
댓글: Jan 2018년 8월 6일
Hi,
I have two vectors that I subtracted from each other and a string of dates that I would like to display in column form like the example below.
Humidity difference for 20/5/18: 54
Humidity difference for 21/5/18: 11
Humidity difference for 22/5/18: 7
Humidity difference for 25/5/18: 29
.
my vectors are;
A=[19.7, 21, 21.4, 26.3, 25.7];
B = [2.2, 2, 4.9, 1.9, 6.6];
C = A-B;
I can display the strings and the vector C individually but they don't relate to each other. If there is an easier way to display this please let me know.
Thanks for the help.
  댓글 수: 1
Jan
Jan 2018년 8월 5일
편집: Jan 2018년 8월 5일
What exactly are your inputs? What is "a string of dates"? What does "display in column" mean? While the important type of the 1st input has not been mentioned, it does not matter in any way, that the values of C are calculated by a subtraction, does it?

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

채택된 답변

Jan
Jan 2018년 8월 5일
DateList = {'20/5/18', '21/5/18'}; % GUESSED input
A = [19.7, 21];
B = [2.2, 2];
C = A - B;
Data = cat(1, DateList, num2cell(C));
sprintf('Humidity difference for %s: %g\n', Data{:});
Result:
Humidity difference for 20/5/18: 17.5
Humidity difference for 21/5/18: 19
  댓글 수: 3
Manuel Flores
Manuel Flores 2018년 8월 6일
Hey Jan
I have another question about the help you provided. The results show up like this,
'Humidity difference for 20/5/18: 17.5
Humidity difference for 21/5/18: 19
'
how do I get rid of the apostrophe at the start and end?
Jan
Jan 2018년 8월 6일
The apostrophes appear only, if you print a char vector to Matlab's command window. They are not part of the string. So simply use a specific function for output, e.g.:
Data = cat(1, DateList, num2cell(C));
Str = sprintf('Humidity difference for %s: %g\n', Data{:});
disp(Str)
fprintf('%s', Str) % Write to command window

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 String에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by