how to read the data of type a*b?

7 vues (au cours des 30 derniers jours)
Mahendra Yadav
Mahendra Yadav le 7 Jan 2025
Commenté : Star Strider le 7 Jan 2025
I have input dataset of following type:
'1*5 5*4 6 8 3 12 -5 9*0 7*-1'
how can I read each value and store it in nice format.
  2 commentaires
Stephen23
Stephen23 le 7 Jan 2025
"how can I read each value and store it in nice format."
What is the expected output for that example?
Mahendra Yadav
Mahendra Yadav le 7 Jan 2025
The output should be an array by consisting all the elments. Like for above input dataset, the output should look like that
x = [1, 5, 5, 4, 6, 8, 3, 12, -5, 9, 0, 7, -1]

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 7 Jan 2025
Modifié(e) : Stephen23 le 7 Jan 2025
str = '1*5 5*4 6 8 3 12 -5 9*0 7*-1';
vec = sscanf(str,'%f%*[ *]',[1,Inf])
vec = 1×13
1 5 5 4 6 8 3 12 -5 9 0 7 -1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  1 commentaire
Mahendra Yadav
Mahendra Yadav le 7 Jan 2025
Thank You Very Much.

Connectez-vous pour commenter.

Plus de réponses (1)

Star Strider
Star Strider le 7 Jan 2025
Taking a wild guess that the ‘multiiplicatiion’ operators are equivalent to the ‘power-of-10’ indicator ‘E’ or ‘e’, first use strrep then strsplit then str2double (here done in a single line) —
format shortG
str = '1*5 5*4 6 8 3 12 -5 9*0 7*-1';
num = str2double(strsplit(strrep(str, '*', 'E'), ' '))
num = 1×9
1.0e+00 * 1e+05 50000 6 8 3 12 -5 9 0.7
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
I have no iidea if this is actually correct.
.
  2 commentaires
Mahendra Yadav
Mahendra Yadav le 7 Jan 2025
Dear S. Strider,
Accutally It's not correct. The above output is not consisting all the elements. Like for the above input dataset, the output should look like that:
x = [1, 5, 5, 4, 6, 8, 3, 12, -5, 9, 0, 7, -1]
Star Strider
Star Strider le 7 Jan 2025
In that instance, then just this —
format shortG
str = '1*5 5*4 6 8 3 12 -5 9*0 7*-1';
num = str2double(strsplit(str, {' ','*'}))
num = 1×13
1 5 5 4 6 8 3 12 -5 9 0 7 -1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Avoid all tthe complications.
I didn’t see this earlier because I was sleeping. I’m in the UTC-7 timezone.
.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Standard File Formats dans Help Center et File Exchange

Produits


Version

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by