MATLAB Answers


proper orientation & writing to text file

Asked by shaz
on 23 Nov 2012

i have a string like

sample1 = ['Software','UserName','Author'];

sample2 = ['Matlab','john','Author1'];

sample3 = ['c','wright','Author2'];

now i need to write to txt file in below format

sample output format

Software    UserName    Author
Matlab      john        Author1
c           wright      Author2

problem is not able to adjust whitespaces




No products are associated with this question.

1 Answer

Answer by Walter Roberson
on 23 Nov 2012
 Accepted answer
sample1 = {'Software','UserName','Author'};  %NOT square brackets!
fprintf('%14s %-14s %14s\n', sample1{:});


Walter Roberson
on 23 Nov 2012

The problem is not with the {} !!

sample1 = {'Software','UserName','Author'}; %NOT square brackets!
fid = fopen('YourOutput.txt', 'wt');
fprintf(fid, '%14s %-14s %14s\n', sample1{:});  %notice first argument!
Jan Simon
on 27 Nov 2012

@Walter: It seems like your comments have lost their context. Did somebody delete some other comments? If so, who and why?

Walter Roberson
on 27 Nov 2012

Looks like all of shaz's comments are gone :(

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi test

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!