obtain answer in brackets

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.

 Accepted Answer

mat2str(magic(3))

17 Comments

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?
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.
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.
disp(mat2str(S_times(1).Times))
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))
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.
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)
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.
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?
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.
Where is that from? Is it part of a row of a table? Is it part of a line in a text file?
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.
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)
That worked. Thank you for your help.
Huh, that was supposed to be 3 hours ago. :)
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.
That makes sense. Thank you!

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 2 Jul 2020
You can use sprintf() to create a string with any appearance you want.
Or fprintf() to display it with any appearance you want.

Community Treasure Hunt

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

Start Hunting!