How are you supposed to write the code returned from "grabcode" to a file?!
1 回表示 (過去 30 日間)
古いコメントを表示
I have lots of published files I am trying to go through and get the code for. I was so happy when I saw that the "grabcode" function returned a string with all the code in it.
My code was this: out=grabocde('myFile.html') and out was a 1x5300 char
My assumption was that this code would have all the linebreaks and whatnot in the code, and when I called disp(out), it displayed perfectly.
My next step was to save this code into a mfile so I could run it. Note that I need to do this thousands of times so opening in the editor and clicking save is not practical.
I simply did fid=fopen('myFile.m','w')
and then just tried fprintf(fid,out) and it didn't do anything, I closed the file and tried opening it and it was blank.
I then re-opened the file and tried: str='darnnn it, why wont you just work'; fprintf(fid,str) and it worked fine.
I closed the file and tried to do grabcode on a different mfile, this time it grabbed the entire first line and then gave me an error about an invalid escape sequence "M" and was done. The other file gave no errors.
And that is pretty much where I am at. I think I do not understand the different ways of having text info in MATLAB, but if somebody could enlighten me, that would be great!
0 件のコメント
採用された回答
Sean de Wolski
2013 年 2 月 20 日
編集済み: Sean de Wolski
2013 年 2 月 20 日
Two easy ways:
You can use the traditional, and probably better, trifecta of fopen/fprintf/fclose
fid = fopen('test2.m','w');
fprintf(fid,'Hello\nWorld'); %your text
fclose(fid);
Or use the editor API:
matlab.desktop.editor.newDocument; %New document
hEditor = matlab.desktop.editor.getActive; %grab the handle to the new document
hEditor.insertTextAtPositionInLine(sprintf('Hello\nWorld'),1,1); %replace sprintf with output from grabcode
hEditor.saveAs([pwd filesep 'test.m']); %save it as whatever
2 件のコメント
その他の回答 (1 件)
参考
カテゴリ
Help Center および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!