Monday 15 June 2015

MATLAB - adding number on text file -


I need to use "importdata" to run a script, but at the top of my file below, more Columns are:

Example 1

  2 2 2 2 2 1 1 1 1 2 4 1 1 2 200000 80000   

this starts with 4 columns, and ends with 5), so when I use the import data, it creates the matrix with 4 columns, the loss of my file Transmits what I want to do It connects any number at the end of the first data row (or second text line), initially 0, it reads my file as a 5-column matrix, such as:

< Strong> Example 1

  2 2 3 2 0 2 2 1 1 0 0 0 0 2 4 0 0 1 2 2 200000 80000   

Zero in other columns, as I think, is the result of "Import Data" in the 5-column matrix, I do not even have to write them. How can this be done?

You can use textscan to read in your code . Here's how to read in your file:

  fid = fopen ('example.txt'); Mat = textscan (fil, '% d% d% d% d% d', 'CollectOutput', 1); Mat = mat {1}; % Cell array matrix reached mat mat (Adnan (mat)) = 0; Sets% NN values ​​to 0 fclose (fid);   

More results:

  mat = 2 2 3 2 0 2 2 1 1 0 1 0 0 0 0 2 4 0 0 1 1 2 200000 80000   

You can save a new file like this:

  fid = fopen ('newfile.txt', 'w'); Fprintf (feeds, '% d% d% d% d% d \ r \ n', mat); Fclose (fid);   

and read it from importdata .

No comments:

Post a Comment