Info

Questa domanda è chiusa. Riaprila per modificarla o per rispondere.

Reading a string and retriving data from the string.

2 visualizzazioni (ultimi 30 giorni)
Michelle
Michelle il 11 Ago 2011
Chiuso: MATLAB Answer Bot il 20 Ago 2021
I have a str='length=12_height=231_width=123' I want to be able to get the three variables, length=12 height=231 width=123 I have some problems reading because I have many different such strings for example, str1='length=122_height=21_width=12.3' where the variable keeps changing and a decimal place i involved. How will I be able to do that? Please advise and thanks in advance!

Risposte (2)

Friedrich
Friedrich il 11 Ago 2011
Hi,
I would use textscan and set the delimiter option to '_'
out = textscan(str1,'%s %s %s','delimiter','_')
After that you will have each variable for his own. But if you really want to create this strings as a variable and assignn the given value, I would do the following
ex_str = strrep(str1,'_',';')
eval(ex_str)

cr
cr il 12 Ago 2011
Another Way:
n = textscan(str,'%s', 'delimiter', '_=');
n = str2double(n{1}(2:2:end))

Questa domanda è chiusa.

Community Treasure Hunt

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

Start Hunting!

Translated by