obtain answer in brackets
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
0 投票
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.
採用された回答
Fangjun Jiang
2020 年 7 月 2 日
mat2str(magic(3))
17 件のコメント
DavidL88
2020 年 7 月 2 日
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
2020 年 7 月 2 日
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
2020 年 7 月 2 日
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
2020 年 7 月 2 日
disp(mat2str(S_times(1).Times))
DavidL88
2020 年 7 月 2 日
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
2020 年 7 月 2 日
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
2020 年 7 月 2 日
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
2020 年 7 月 2 日
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
2020 年 7 月 2 日
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
2020 年 7 月 2 日
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
2020 年 7 月 2 日
Where is that from? Is it part of a row of a table? Is it part of a line in a text file?
DavidL88
2020 年 7 月 2 日
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
2020 年 7 月 2 日
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
2020 年 7 月 2 日
That worked. Thank you for your help.
Fangjun Jiang
2020 年 7 月 2 日
Huh, that was supposed to be 3 hours ago. :)
Image Analyst
2020 年 7 月 2 日
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
2020 年 7 月 2 日
That makes sense. Thank you!
その他の回答 (1 件)
Image Analyst
2020 年 7 月 2 日
You can use sprintf() to create a string with any appearance you want.
Or fprintf() to display it with any appearance you want.
カテゴリ
ヘルプ センター および File Exchange で Logical についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
