How do I print a string and an array on the same line or with one function call?
24 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
MathWorks Support Team
am 13 Jun. 2024
Beantwortet: MathWorks Support Team
am 21 Jun. 2024
Currently, I have to write two lines of code as shown below to display a string followed by the values in my array. I do this by calling "disp" twice:
>> disp('Your array is')
Your array is
>> disp([1 2; 3 4])
1 2
3 4
I am looking for a faster and easier way to display a string and then an array.
It would be great if there was a MATLAB function that could do this. Having a MATLAB function to do this in one line would improve the simplicity of my code while saving me a lot of time and effort.
Akzeptierte Antwort
MathWorks Support Team
am 13 Jun. 2024
There are a couple of workarounds you can use to print a string and then an array in one function call:
1. Use the "sprintf" command with "%s" as shown below, where A = [1 2; 3 4]:
>> sprintf("Your array is %s", mat2str(A))
ans =
"Your array is [1 2;3 4]"
2. Create your own function that achieves the output you desire, and add the folder holding this function to your MATLAB search path to call this function anywhere.
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Dates and Time finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!