Discover MakerZone

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

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to save text files in Matlab GUI from edit text box

Subject: How to save text files in Matlab GUI from edit text box

From: Matteo Lorenzini

Date: 4 Jun, 2014 10:00:17

Message: 1 of 2

Dear all,
please help me in understanding the usage of GUI. I found on the web a suggested way to write a button callback to read the content of an edit text box and save to a txt file, that is:

%save text of command.txt file
fid = fopen('command.txt','wt');
fprintf(fid,'%c',get(handles.edit1,'String'))
fclose(fid);

Anyway, the text in the box is saved in the text file in a wrong and very peculiar way; if I write in the box something like:

PALA
POLO

I find in the command.txt file has been saved the following:

PPAOLLAO

and I can't manage to obtain more than one line. Any help or suggestion is welcome, thanks!

Subject: How to save text files in Matlab GUI from edit text box

From: Steven Lord

Date: 4 Jun, 2014 14:31:31

Message: 2 of 2


"Matteo Lorenzini" <matteo.lorenzini@roma2.infn.it> wrote in message
news:lmmqnh$muc$1@newscl01ah.mathworks.com...
> Dear all,
> please help me in understanding the usage of GUI. I found on the web a
> suggested way to write a button callback to read the content of an edit
> text box and save to a txt file, that is:
>
> %save text of command.txt file
> fid = fopen('command.txt','wt');
> fprintf(fid,'%c',get(handles.edit1,'String'))
> fclose(fid);
>
> Anyway, the text in the box is saved in the text file in a wrong and very
> peculiar way; if I write in the box something like:
>
> PALA
> POLO
>
> I find in the command.txt file has been saved the following:
>
> PPAOLLAO

That's correct. MATLAB is column major, and so when FPRINTF iterates through
the character matrix:

['PALA'; 'POLO']

the first element is 'P' and the second element is also 'P'. The third
element is the first 'A' in the first line. The ordering of elements is:

reshape(1:8, [2 4])

There are a couple different ways to handle this depending on your level of
experience with MATLAB. You could write each row at a time, or you could use
a cell array of strings instead of a character matrix.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us