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

need function to save my data

Asked by huda nawaf on 14 Sep 2012

hi,

I have data is formatted as

*cluster1 2 3

cluster2 1 4 5

cluster3 6 7 10 8*

I would like to find way to save it in file, I know textscan work with such format but this function need equal number of columns in each row.

is there any other way to do that?

thanks

4 Comments

huda nawaf on 14 Sep 2012

yes walter, it is working I were not write it correctly.

But what i need now is considering the characters is first string and the digits is the second string because I can not continue write %d%d%d..... if cluster has many numbers of objects So, I did the following

       f=fopen('train.txt');
       c=textscan(f,'%s %s' );
       c1=c{1};c2=c{2};

it is working well, but the second string can not converted to number later

I used for example: x=str2num(c2(1)) it must get

2 3 but i got ??? Error using ==> str2num at 33 Requires string or character array input.

thanks

Walter Roberson on 14 Sep 2012

Try str2num(c2{1})

huda nawaf on 14 Sep 2012

thanks when I do that , it still deal with 23 as string where when need x1{1}(1) will give me 23 not 2

huda nawaf

Tags

Products

No products are associated with this question.

4 Answers

Answer by per isakson on 14 Sep 2012
Edited by per isakson on 15 Sep 2012

I assume that

  1. you want to import data from a text file, which is defined by the three rows embraced by "*"
  2. the "*" are traces of your effort to format the text

Since the rows have different formats textscan is not appropriate.

One way to read the file is to loop over the all rows and read one row at a time with the function

    fgetl, Read line from file, removing newline characters

There is an example in the documentation

.

-- Working code [ R2012a, 2012-09-15 ] ---

    >> S = cssm()
    S = 
    1x3 struct array with fields:
        Cluster
        Items
    >> [S.Cluster]
    ans =
         1     2     3
    >> S(3).Items
    ans =
         6     7    10     8

where cssm.m is

    function S = cssm()
        %%
        fid = fopen('cssm.txt');
        cup = onCleanup( @() fclose( fid ) );
        %%
        S   = struct([]);
        str = fgetl( fid );
        while ischar( str )
            str = strtrim( str );
            str = strrep( str, 'cluster', '' );
            num = str2num( str );
            S(end+1).Cluster = num(1); %#ok<AGROW>
            S(end  ).Items   = num(2:end); 
            str = fgetl( fid );     
        end
    end

and where cssm.txt contains the tree rows

    cluster1 2 3
    cluster2 1 4 5
    cluster3 6 7 10 8 

.

--- Alternate code ---

    >> S = cssm()
    S = 
    1x3 struct array with fields:
        RowHeader
        Items
    >> {S.RowHeader}
    ans = 
        'cluster1'    'cluster2'    'cluster3'
    >> S(3).Items
    ans =
         6     7    10     8

where cssm.m is

    function S = cssm()
        %%
        fid = fopen('cssm.txt');
        cup = onCleanup( @() fclose( fid ) );
        %%
        S   = struct([]);
        str = fgetl( fid );
        while ischar( str )
            [ tok, str ] = strtok( strtrim( str ) );
            S(end+1).RowHeader  = tok; %#ok<AGROW>
            S(end  ).Items      = str2num( str ); 
            str = fgetl( fid );     
        end
    end

1 Comment

per isakson on 15 Sep 2012

Working code added

per isakson
Answer by Pritesh Shah on 14 Sep 2012

Just find help save and load function.

If you want to save data....

Assuming that it is already available in work space.

0 Comments

Pritesh Shah
Answer by Image Analyst on 15 Sep 2012

And the problem with using save() is......??????? What????

0 Comments

Image Analyst
Answer by Pritesh Shah on 7 Dec 2012

Use save function to store variables.

Help save

Enjoy !!

0 Comments

Pritesh Shah

Contact us