|
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
|