strcat('Scale Factor = ',num2str(table2array(T(min(find(find_groups==k)),'scalefactor'))))
and the output is like:
'Scale Factor =0.65'
I need to add the space between = and 0.65. Using above code does not add the space. Any idea/solution?

댓글 수: 1

Please note in Walter's answer how he simplified the horror that is:
table2array(T(min(find(find_groups==k)),'scalefactor')))
to
T.scalefactor(find(find_groups==k,1)))
Use proper table indexing,
table2array(Atable(rows, 'variablename')) %() indexing on a table returns a table
is simply
Atable.variablename(rows) %. indexing returns the content of the table
%or
Atable{rows, 'variablename'} % {} indexing returns the content of the table
and
min(find(something))
is simply
find(something, 1) %get the first element only

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

 채택된 답변

Image Analyst
Image Analyst 2020년 1월 29일

1 개 추천

Try fprintf()
theNumber = table2array(T(min(find(find_groups==k)),'scalefactor')); % Whatever...but don't use num2str()
fprintf('Scale Factor = %.2f.\n', theNumber);

댓글 수: 6

Zeynab Mousavikhamene
Zeynab Mousavikhamene 2020년 1월 29일
Thanks but I dont want to print it. I want to use it as plot x axis title.
theNumber = table2array(T(min(find(find_groups==k)),'scalefactor')); % Whatever...but don't use num2str()
xLabelString = sprintf('Scale Factor = %.2f.\n', theNumber);
xlabel(xLabelString, 'FontSize', 20);
Though sprintf is a much better approach, you can also use [] or horzcat to concatenate pretty much any like class of variables; in this case character vectors.
xLabelString = ['Scale Factor = ',num2str(table2array(T(min(find(find_groups==k)),'scalefactor')))];
Thank you. And if it was string array, how could I add no limitation to the resulting string? for example
xLabelString = sprintf('Name = %.2s', theName);
this line says report the string until two characters. so if the name is "Hello" it would report "He". How can I say to report the whole "Hello" not part of it?
Ps. Hello is an example and the length of name might change in each iteration so I need a general method.
If you don't put any restriction on the format length, then matlab won't truncate the input. So
sprintf('Name = %s', theName) %no limit to 2 characters
would work.
If you want a minimum field width of 2 characters but more if required, then
sprintf('Name = %2s', theName) %No . in the format string. Minimum field width of 2, padding with spaces if required
Zeynab Mousavikhamene
Zeynab Mousavikhamene 2020년 1월 29일
Thank you @Image Analyst and @Allen and @Guillaume. If you post separately I can vote for your posts.

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

추가 답변 (2개)

Walter Roberson
Walter Roberson 2020년 1월 29일

1 개 추천

For character array inputs, strcat removes trailing ASCII white-space characters: space, tab, vertical tab, newline, carriage return, and form feed. For cell and string array inputs, strcat does not remove trailing white space.

댓글 수: 2

Zeynab Mousavikhamene
Zeynab Mousavikhamene 2020년 1월 29일
so what would you suggest to do?
strcat({'Scale Factor = '}, num2str(T.scalefactor(find(find_groups==k,1))))

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

Steven Lord
Steven Lord 2020년 1월 29일

1 개 추천

If you're using a release that supports string arrays, you can simplify this a bit if you trust MATLAB to do "something reasonable" when combining your text and numeric data.
x = 0.65;
S = "Name = " + x

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by