Hi
I have some data in a 3x1 struct with 2 fields. The data is stored in this fashion [0.1000,0.2500]. When I retrieve the dat using this code Times_period = S_times(1).Times I get ans = 0.1000 0.2500. I want to retieve exactly this [0.1000,0.2500] (ie with square brackets included). Can anyone advsie how I do this?
I tried Times_period = {"[",S_times(1).Times,"]"}. But I get this = 1×3 cell array {["["]} {1×2 double} {["]"]}
Tried other variations but no success.

 Respuesta aceptada

Fangjun Jiang
Fangjun Jiang el 2 de Jul. de 2020

1 voto

mat2str(magic(3))

17 comentarios

DavidL88
DavidL88 el 2 de Jul. de 2020
Hi Fangjun
Thank you. I ran this code Times_period = mat2str(S_times(1).Times) and got this = '[0.1 0.25]'
Could you advise how I can adjust it to get it without the aprotophes?
Fangjun Jiang
Fangjun Jiang el 2 de Jul. de 2020
The single quote indicates the data type of the data. It is not part of the data. Where do you want [0.1000,0.2500] to be put in? You could do disp(Times_period) in Command Window and it will show that.
DavidL88
DavidL88 el 2 de Jul. de 2020
I'm running a statistcal analysis and need to run a loop on times. For the stats code to run it needs to be in this format exactly - [0.1 0.25]. I ran disp(S_times(1).Times) and got 0.1 0.25.
Fangjun Jiang
Fangjun Jiang el 2 de Jul. de 2020
disp(mat2str(S_times(1).Times))
DavidL88
DavidL88 el 2 de Jul. de 2020
Cheers that works. To clarify when I run disp(mat2str(S_times(1).Times)) I get the exact right answer. But when I run Times_period = disp(mat2str(S_times(1).Times)) I get this below. Could you tell me why?
Error using disp
Too many output arguments.
Error in Untitled2 (line 2)
Times_period = disp(mat2str(S_times(i).Times))
DavidL88
DavidL88 el 2 de Jul. de 2020
I'm trying to run it as part of a loop.
This code
for i = 1:length(S_times)
disp(mat2str(S_times(i).Times))
end
returns these values
[0.1 0.25]
[0.25 0.5]
[0.5 0.9]
But it won't work in the statistics script. when I run the statistics script I get - Error using disp Too many output arguments.
Fangjun Jiang
Fangjun Jiang el 2 de Jul. de 2020
that is why I ask where you want to the [0.1 0.25] be put in? a text file, or any particular format, to interface with your statistcal analysis tool? disp() is to display the data to MATLAB Command Window. It does not allow return variable.
Most likely, you need to use Times_period = sprintf('[%d %d]',S_times(1).Times)
DavidL88
DavidL88 el 2 de Jul. de 2020
Seems to be on the right track. When I run Times_period = sprintf('[%.3f %.3f]',S_times(i).Times) I get '[0.100 0.250]'. I can't get it to work without the apostrophes. The statistics script won't run if apostrophes are included.
Fangjun Jiang
Fangjun Jiang el 2 de Jul. de 2020
We are going in a circle now. mat2str() gives you the same result as sprintf() without knowing the complex syntax of sprintf(). Both give a CHAR data type which is indicated by ' and '.
How do you interface with the statistics script? Could you provide details?
DavidL88
DavidL88 el 2 de Jul. de 2020
The statistics script is for a GUI Brainstorm which is based on MatLab. Here's an example of the parameter in the statistics script in MatLab that I need to run the loop through.
'timewindow', [0.1, 0.25], ...
The data needs to be in that format. The idea is to place Times_period instead of [0.1, 0.25] so I can loop multiple times through this script. Please let me know if you need more information.
Image Analyst
Image Analyst el 2 de Jul. de 2020
Where is that from? Is it part of a row of a table? Is it part of a line in a text file?
DavidL88
DavidL88 el 2 de Jul. de 2020
It's a line in a MatLab script to run a statistical analysis in Brainstorm (software for analysing EEG data based on MatLab). Brainstorm can generates Matlab scripts to adjust and run statistics analyses.
Fangjun Jiang
Fangjun Jiang el 2 de Jul. de 2020
You need to be straightforward right at the begining and provide as much details as possible. If the script is in MATLAB, most likely, you've wasted a lot of time struggling with this.
If your statistics script is called like RunScript('timewindow', [0.1 0.25])
then all you need to do is RunScript('timewindow',S_times(i).Times)
DavidL88
DavidL88 el 2 de Jul. de 2020
That worked. Thank you for your help.
Fangjun Jiang
Fangjun Jiang el 2 de Jul. de 2020
Huh, that was supposed to be 3 hours ago. :)
Image Analyst
Image Analyst el 2 de Jul. de 2020
They used brackets in that line of code -- [0.1, 0.25] -- because they needed to create an array (a row vector) from two constants. Like Fangjun said, if you already have this array it wants in your S_times(i).Times, then you can just use that:
'timewindow', S_times(i).Times, ...
There are really no brackets - that's just how you construct an array but they are not actually part of the array itself -- the array is just numbers alone.
DavidL88
DavidL88 el 2 de Jul. de 2020
That makes sense. Thank you!

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 2 de Jul. de 2020

0 votos

You can use sprintf() to create a string with any appearance you want.
Or fprintf() to display it with any appearance you want.

Categorías

Etiquetas

Preguntada:

el 2 de Jul. de 2020

Comentada:

el 2 de Jul. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by