String and character array not properly printing

I am trying to write a prompt for input from the user in a subfunction. This prompt will be dependent upon the name of the files they have loaded in another subfunction. I can get Matlab to correctly write the string (as shown when I look at the variable while debugging) using the following code,
function GraphIt (filenamestr)
promptopt=strings(1,length(filenamestr)); %preallocating
prompt=sprintf('Which file do you want to graph from? (Please input number)'); %first line of prompt
for i=1:length(filenamestr) %Writing string
promptopt=sprintf(' %d. %s',i,filenamestr(i));
prompt=strvcat(prompt,promptopt);
end
funcstr=input(prompt); %User input
However I cannot get the string to properly print via fprintf or the prompt of the input. I have tried using the char command (with \n at the end of each line) instead of strvcat to write the text as a character array but I still have the same problem.
Please note that the string has characters such as ()-, in it.
For reference the string prints as:
W h12i..c h0L .if1Bi NlMMe B B dLmoDi -cyIro-ou1e 2lw0ea cnpttor lotydo(e L gi6rBVaN pMchBy )cf lr(io1nm.g?5. t(mxPLtl) e am si en i i nb pa ut th nt ue ms bt e, r )i n s i d e g l o v e b o x , t e f l o n c a p , P t m i c r o e l e c t r o d e , 6 V 5 0 c y c l e s , 0 . 0 1 V s . t x t W h12i..c h0L .if1Bi NlMMe B B dLmoDi -cyIro-ou1e 2lw0ea cnpttor lotydo(e L gi6rBVaN pMchBy )cf lr(io1nm.g?5. t(mxPLtl) e am si en i i nb pa ut th nt ue ms bt e, r )i n s i d e g l o v e b o x , t e f l o n c a p , P t m i c r o e l e c t r o d e , 6 V 5 0 c y c l e s , 0 . 0 1 V s . t x t
when it should be:
Which file do you want to graph from? (Please input number)
1. 0.1 M BLD-I-120 poly(LiBNMB) (1.5 mL) mini bath test, inside glovebox, teflon cap, Pt microelectrode, 6 V 50 cycles, 0.01Vs.txt
2. LiBNMB microelectrode 6V cycling.txt
Thanks for your help!

 Réponse acceptée

Stephen23
Stephen23 le 28 Jan 2018
Modifié(e) : Stephen23 le 28 Jan 2018
Two things happen here: input actually only accepts a character vector for prompt but you are providing it with a matrix, and you then failed to take into account the fact that MATLAB is column-major (so that when you provide a non-supported matrix prompt to input then MATLAB converts this into one row character vector using the column-major sequence). Your characters are certainly all there, you can clearly see how they have been arranged:
W h12i..c h0L .if1Bi NlMMe B B dLmoDi -cyIro-ou1e 2lw0ea cnpttor lotydo(e ...
row 1: W h i c h f i l e d o y o u w a n t t o ...
row 2: 1 . 0 . 1 M B L D - I - 1 2 0 p o l y ( ...
row 3: 2 . L i B N M B m i c r o e l e c t r o d e ...
The solution is to not use strvcat, but concatenate the substrings properly into a single vector, so that you can provide a character vector just as the input help requests. You should always use the 's' option too, to avoid magically evaluating whatever input the user gives. Here is one solution (untested):
function GraphIt(filenamestr)
prompt = 'Which file do you want to graph from? (Please input number)');
for k = 1:numel(filenamestr)
prompt = sprintf('%s\n %d. %s', prompt, k, filenamestr(k));
end
funcstr = input(prompt,'s');

Plus de réponses (0)

Catégories

En savoir plus sur Mathematics dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by