Thread Subject: Flavour/version of the 'disp' command...

Subject: Flavour/version of the 'disp' command...

From: Masood Sadiq

Date: 4 Nov, 2008 11:45:03

Message: 1 of 4

Hello Everyone:

I am new to Matlab so please help.

I am looking for a "flavour" of the 'disp' command that will allow me to display the contents of two arrays (char_U and char_count) side by side (i.e. not one after the other).

My program takes an input file, file1.dat, and does a word count for each unique word. The program in full is:
-----------------------------------------------
fid1 = fopen('file1.dat', 'rt');
WordList1 = textread('file1.dat', '%s');
fclose(fid1);
Sorted1 = sort(lower(WordList1));
[U,unused,index] = unique(Sorted1);
count = accumarray(index(:),1);

% create character arrays - do conversions
char_count = num2str(count);
char_U = char(U);
 
% to improve format of displayed output add an '=' before each count
char_count = strcat(' =',char_count);
 
% Now I need to display char_U and char_count next to each
%other, so that it appears as (for example):
% be =2
% was =1 , etc
----------------------------------------------------
Can anyone help?
Cheers
Masood

Subject: Flavour/version of the 'disp' command...

From: vedenev

Date: 4 Nov, 2008 13:23:48

Message: 2 of 4

Example:
disp([ char(43) '+' ' kjgklj']);

in matlab string is vector of chars in this example 3 strings vas
connected:
char(43)
'+'
' kjgklj'

------------------------------------
Maxim Vedenev, Matlab freelancer
http://simulations.narod.ru/

Subject: Flavour/version of the 'disp' command...

From: Kelly

Date: 4 Nov, 2008 16:38:42

Message: 3 of 4

On Nov 4, 6:45 am, "Masood Sadiq" <masood.sa...@power.alstom.com>
wrote:
> Hello Everyone:
>
> I am new to Matlab so please help.
>
> I am looking for a "flavour" of the 'disp' command that will allow me to display the contents of two arrays (char_U and char_count) side by side (i.e. not one after the other).
>
> My program takes an input file, file1.dat, and does a word count for each unique word. The program in full is:
> -----------------------------------------------
> fid1 = fopen('file1.dat', 'rt');
> WordList1 = textread('file1.dat', '%s');
> fclose(fid1);
> Sorted1 = sort(lower(WordList1));
> [U,unused,index] = unique(Sorted1);
> count = accumarray(index(:),1);
>
> % create character arrays - do conversions
> char_count = num2str(count);
> char_U = char(U);
>
> % to improve format of displayed output add an '=' before each count
> char_count = strcat(' =',char_count);
>
> % Now I need to display char_U and char_count next to each
> %other, so that it appears as (for example):
> % be =2
> % was =1 , etc
> ----------------------------------------------------
> Can anyone help?
> Cheers
> Masood


The sprintf function can be used to format your strings and numbers
into a single string. Also, you may want to take a look at aprintf,
on the FEX. It allows for customized display of cell arrays. For
your case:

U = {'alpha'; 'bravo'; 'charlie'; 'delta'}; count = [1; 2; 3; 4];

% Make a cell array and display using default Matlab behavior

>> [U num2cell(count)]

ans =

    'alpha' [1]
    'bravo' [2]
    'charlie' [3]
    'delta' [4]

% Customized display of same cell array

aprintf([U num2cell(count)], '-d', ' = ', '-n', '%d')

ans =

  alpha = 1
  bravo = 2
charlie = 3
  delta = 4

Subject: Flavour/version of the 'disp' command...

From: Masood Sadiq

Date: 5 Nov, 2008 09:53:01

Message: 4 of 4

Thanks, Kelly and Vedenev! The problem is now sorted thanks to your co-operation.
Cheers
Masood

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
disp command Masood S 4 Nov, 2008 06:50:04
rssFeed for this Thread

Contact us at files@mathworks.com