Matlab reading data file

okd
okd used Ask the Experts™
on
I need more help in reading data file in Matlab. The data files are like below. I want to read this data horizontally and store them in a variable. I also need to count the number of data. Finally, I want to define another variable or array which has same number of data that starts with 0.1 and incremented by 0.3. I need the code with little explanation. Thanks.

1  2  3  4  4  6
7  8   8  9  7  6
-------------------
-------------------
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
I assume the data file is tab-delimited text file (please specify it exactly next time).
Here is the code with comments.
As for another variable your question was a little ambiguous, so the code with generate several. Choose what you need.
Should work but I didn't test in Matlab. Let me know if something doesn't work.

data = dlmread('data.txt'); % reading the data file (you can specify delimiter as 2nd argument)
n_data = numel(data); % number of data elements
[n_row n_col] = size(data); % number of rows and columns
 
% create another arrays
data2 = 0.1:0.3:n_data; % same number of elements
data3 = reshape(size(data),0.1:0.3:n_data); % same size
data4 = 0.1:0.3:n_row; % vector with same number of rows
data5 = 0.1:0.3:n_col; % vector with same number of columns

Open in new window

okd

Author

Commented:
My data looks something like below. Col A data which I read from the file. Which is ok.
The Col B data which I will have to generate. It will start from 0.1 and incremented by 0.02
by the n_data times. Therefore, I need the n_data value also (There is some error in the n_data command as given below). Finally, I want to use Col A and Col B
data for plotting integration, Fourier Transforms, etc. Please let me
know if I could explan this well. Thanks.

Col  A            Col  B
-2.257      0.1
-0.708      0.12
0.339      0.14
-1.153      0.16
-4.317      0.18
-5.423      0.2
9.006      0.22
6.764      0.24
3.293      0.26
3.088      0.28
5.707      0.3
1.915      0.32

---------------------------
Matlab outputs

» data1=dlmread('C:\Documents and Settings\DDR\My Documents\Ltest.txt','\t')

data1 =

   -2.2570
   -0.7080
    0.3390
   -1.1530
   -4.3170
   -5.4230
    9.0060
    6.7640
    3.2930
    3.0880
    5.7070
    1.9150

» n_data=numel(data1)
??? Undefined function or variable 'numel'.

Stuck after this.
Commented:
That is strange. NUMEL is standard build-in Matlab function. It returns number of elements in argument variable. Which Matlab version are you using? Type ver in Matlab command prompt to get it.

Also you can try this code instead:
dstart = 0.1;
dstep = 0.02;
n = length(data1);
dend = dstart+dstep*(n-1);
data2 = (dstart:dstep:dend)';
data1 = [data1 data2]; % add data2 as a second column to data1

Open in new window

okd

Author

Commented:
My MATLAB version is 5.3. Anyway, all worked now. Thank you very much for your help. I will open another question with the Fourier transform on the same data. I need some clarification on the variables used there. Thank you again.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial