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:
create a table using .txt file

Subject: create a table using .txt file

From: ashish

Date: 23 Nov, 2008 19:27:02

Message: 1 of 3

I have loaded .txt file and stored it in a variable. How do i create a table using that data? I need to label each column and give it a title.
Below is the .txt data
1732 2 22
1777 12 15
1778 1 20
1564 4 23
1592 2 21
1592 4 2
2000 2 1
2008 11 3
2009 1 15


Thank you.

Subject: create a table using .txt file

From: Walter Roberson

Date: 23 Nov, 2008 19:34:23

Message: 2 of 3

ashish wrote:
> I have loaded .txt file and stored it in a variable. How do i create a table using that data? I need to label each column and give it a title.
> Below is the .txt data
> 1732 2 22
> 1777 12 15
> 1778 1 20
> 1564 4 23
> 1592 2 21
> 1592 4 2
> 2000 2 1
> 2008 11 3
> 2009 1 15

Which version do you have? If you have R2008b, you can use the (newly official)
uitable(); you can probably use num2cell() to get the proper cell array to feed in.
(A usable uitable might also be in R2008a; before that I believe the uitable
was undocumented and had notable problems.)

--
.signature note: I am now avoiding replying to unclear or ambiguous postings.
Please review questions before posting them. Be specific. Use examples of what you mean,
of what you don't mean. Specify boundary conditions, and data classes and value
relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?

Subject: create a table using .txt file

From: Andres

Date: 23 Nov, 2008 20:04:02

Message: 3 of 3

"ashish " <ashishtx@hotmail.com> wrote in message <ggcaq6$h65$1@fred.mathworks.com>...
> I have loaded .txt file and stored it in a variable. How do i create a table using that data? I need to label each column and give it a title.

By "table" you mean a new file? Then just use fprintf.

d = [1732 2 22
1777 12 15
1778 1 20
1564 4 23
1592 2 21
1592 4 2
2000 2 1
2008 11 3
2009 1 15];

titles = '"Title1","Title2","Title3"';

% write to file with pc type line breaks ('\r\n'):
fid = fopen('c:\mytable.csv','w');
fprintf(fid, '%s\r\n', titles);
fprintf(fid, '%f,%f,%f\r\n', d);
fclose(fid)


Such a csv file should open correctly in standard spreadsheet software, like OpenOffice. If it is MS Excel, of course, you may look at xlswrite and use a single cell array.

Regards
Andres

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