problem using fprintf for writing cell data and global data at the same time.
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
if ~ischar(fileName)
return;
end
fileID = fopen(fullfile(filePath, fileName), 'w');
cellArray=
'p.4004'
'p.4005'
'p.4007'
cellArray 3x1 cell global
matrix=[1 2 3;4 5 6;7 8 9]
%I can write these values separetely into word like this;
fprintf(fileID,'%47f %f \n',matrix')
fprintf(fileID,'%s\n',cellArray{:})
%but I need to write them together into microsoft word like this;
p.4004 1 2 3
p.4005 4 5 6
p.4007 7 8 9
Accepted Answer
Azzi Abdelmalek
on 3 Aug 2013
cellArray={'p.4004'
'p.4005'
'p.4007'}
matrix=[1 2 3;4 5 6;7 8 9]
v=[cellArray num2cell(matrix)]
fileID = fopen('fic13.txt', 'w');
for k=1:size(v,1)
fprintf(fileID, '%s %d %d %d\n', v{k,:});
end
fclose(fileID)
13 Comments
sermet
on 3 Aug 2013
it gives this result;
p.4004 1 2 3 p.4005 4 5 6 p.4007 7 8 9
I want to create like this;
p.4004 1 2 3
p.4005 4 5 6
p.4007 7 8 9
Azzi Abdelmalek
on 3 Aug 2013
That's what the code do. Have you copied it then run it?
sermet
on 3 Aug 2013
yes of course.
Azzi Abdelmalek
on 3 Aug 2013
Edited: Azzi Abdelmalek
on 3 Aug 2013
open the file with wordpad, it's different from notepad
sermet
on 3 Aug 2013
it cannot separate the p.'s and the numbers it writes them side to side.
sermet
on 3 Aug 2013
i mean first column needs to be only p.'s and then the second and third columns includes numbers.
sermet
on 3 Aug 2013
I opened microsoft word not note bloc.
Azzi Abdelmalek
on 3 Aug 2013
Edited: Azzi Abdelmalek
on 3 Aug 2013
Try this for notepad (\r\n)
cellArray={'p.4004'
'p.4005'
'p.4007'}
matrix=[1 2 3;4 5 6;7 8 9]
v=[cellArray num2cell(matrix)]
fileID = fopen('fic13.txt', 'w');
for k=1:size(v,1)
fprintf(fileID, '%s %d %d %d\r\n', v{k,:});
end
fclose(fileID)
sermet
on 3 Aug 2013
it gives the same result sir.
Azzi Abdelmalek
on 3 Aug 2013
What is your Operating System?
sermet
on 3 Aug 2013
windows 7 64 bit, matlab 2012
Azzi Abdelmalek
on 3 Aug 2013
I don't know what is the problem, when I run the same code, I'am getting the correct answer. Maybe you are checking the wrong file?
sermet
on 3 Aug 2013
ok no problem, I'll try it again carefully, thank you so much for sparing your time.
More Answers (0)
Categories
Find more on Low-Level File I/O in Help Center and File Exchange
Tags
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)