[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How to write the output of the code to .csv format and save the file
From: |
N |
Subject: |
Re: How to write the output of the code to .csv format and save the file |
Date: |
Sun, 7 Jun 2020 14:15:42 +0200 |
> > yes, I agree.
> >
> > I am not able to figure out how to write it.
> >
> > Is it possible to help me out in a sample code
>
> You have to learn about printf(...) function style formatting, it is old and
> have benn used for decades, actually quite many. % is used for comment, there
> is indentation for both code/comment and if I remember it correct is
> something like below:
>
>
> [fid msg] = fopen('fileame', 'r'); % Open file
> if(fid == -1) % Error occured?
> error(msg) % Throw error and show
> error messsage what went wrong
> end
>
> try % Start try block so that
> error could be catched and file closed
> fprintf(fid, 'column2; column2; column n\n'); % Here you may add code
> to write header or similar
> for n = 1:rows % For all rows you want
> to save data
> fprintf(fid, 'column2; column2; column n\n'); % Here you have to add
> code to write data, probably using a for loop
> end
> flcose(fid); % Close file
> catch err % Catch error in case it
> happen, rows below are not run then everything is OK
> fclose(fid); % This is needed
> otherwise file is not closed then you make error in code and it is a problem
> rethrow(err) % Rethrow the same error
> but here file have been closed before error
> end
>
>
> Nicklas SB Karlsson
Discovered I made an error fopen('fileame', 'r') open in read only, it must be
opened in write mode so it should be 'w' instead of 'r':
[fid msg] = fopen('fileame', 'w'); % Open file