How to save structure as .asc?

Hello. I have a structure that looks like this:
And i whant to save these information in a .asc file. In the end the file will look like this:
I tried with printf but without results. Can you help me? I'm novice in Matlab.
Thanks

3 件のコメント

Rik
Rik 2017 年 10 月 24 日
fprintf is the command you're looking for. If you get an error, show the code you are using and the complete error message.
knoppyx
knoppyx 2017 年 10 月 24 日
I tried:
fileID='blabla.asc';
fileID = fopen(fileID,'w');
fprintf(fileID,param.textdata,param.data);
fclose(fileID);
But the error is: Error using fprintf Invalid format.
Stephen23
Stephen23 2017 年 10 月 24 日
@knoppyx: You have not specified any format string. Read the fprintf help to know how to specify the format string.

サインインしてコメントする。

回答 (1 件)

KSSV
KSSV 2017 年 10 月 24 日

1 投票

S{1} = 'IMC FAMOS' ;
S{2} = 'NAME: Wheelspeed_R_L' ;
S{3} = 'Time: 18/08/16 11:28:46' ;
S{4} = 'Length: 21456' ;
S{5} = 'unit X-Axis: 1 Samples' ;
S{6} = '1st x-Value: 0.0089 samples' ;
S{7} = 'unit Y-Axis: rpm' ;
data = rand(21456,2) ; % some random data
fid = fopen('myfile.asc','w') ;
for i = 1:length(S)
fprintf(fid,'%s\n',S{i}) ;
end
fprintf(fid,'%s\n','Data:') ;
fprintf(fid,'%f %f\n',data) ;
fclose(fid) ;

8 件のコメント

Jan
Jan 2017 年 10 月 24 日
Or without a loop:
fprintf(fid, '%s\n', S{:});
knoppyx
knoppyx 2017 年 10 月 24 日
I tried:
fileID=fisiere(j).name;
fileID = fopen(fileID,'w');
fprintf(fileID,'%s\n',param.textdata{:});
And the result is:
I want to show me each line underneath. That's not what "\n" do?
Rik
Rik 2017 年 10 月 24 日
Welcome in the world of end-of-line markers. In notepad you will need \r\n instead of \n to see a new line. If you would have opened it in e.g. Notepad++, you would have seen new lines. This is an oddity in Windows that is very old and will probably not change in the coming decade.
Jan
Jan 2017 年 10 月 24 日
編集済み: Jan 2017 年 10 月 24 日
No editor I know besides Windows Notepad suffers from \n line breaks. Either use WordPad, Notepad++ or Matlab's editor, or use \r\n as mentioned by Rik already, if you really prefer the darn NotePad.
@Rik: Who knows - they have added the feature to copy&paste in the command shell, although this method is common for 30 years now.
Stephen23
Stephen23 2017 年 10 月 24 日
Actually the only change that is required is to open the file in text mode. This will take care of the correct newline characters:
fid = fopen('myfile.asc','wt');
^ use this to open in text mode!
knoppyx
knoppyx 2017 年 10 月 25 日
編集済み: knoppyx 2017 年 10 月 25 日
Thank you, it works. But the "param.data" is not looking very good.
fileID=fisiere(4).name;
fileID = fopen(fileID,'wt');
fprintf(fileID,'%s\n',param.textdata{:});
fprintf(fileID,'%s\n',param.data(:));
fclose(fileID);
It looks like this:
And i want to look like this:
How can i change the code to obtain that result?
All i whant is that the first colum from param.data to be the firs colum from resulted file and the second colum from param.data to be the second colum from resulted file.
Stephen23
Stephen23 2017 年 10 月 25 日
編集済み: Stephen23 2017 年 10 月 25 日
@knoppyx: one day ago I wrote that you should "Read the fprintf help to know how to specify the format string." Have you read the documentation to learn how to specify different format strings?
You will need to transpose the input matrix, and specify format that suits your needs. Something like this:
fprintf(fileID,'%.4f%11.4f\n',param.data.');
You need to read the fprintf documentation carefully, because there are lots of options for specifying the format string.
knoppyx
knoppyx 2017 年 10 月 25 日
i've solved it, thank you. You were right, i've solved it with the help of transpose.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeData Import and Analysis についてさらに検索

質問済み:

2017 年 10 月 24 日

コメント済み:

2017 年 10 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by