フィルターのクリア

I have a text file that contains 4*10 array. I want to read 1st column element first and store that column in say x, process it and then read 2nd column and store in same variable x. Is there any commands for that? please help?

3 ビュー (過去 30 日間)
-1.9360 20.7501 -1.3800 0.2735 -1.9360 20.7501 -1.3800 0.2735 20.7501 -1.3800
-2.5797 27.6486 -1.3819 0.2524 -2.5797 27.6486 -1.3819 0.2524 27.6486 -1.3819
1.5073 28.2723 -0.0000 15.0002 1.5073 28.2723 -0.0000 15.0002 -0.0000 15.0002
-1.9770 37.0812 0.0000 15.0009 -1.9770 37.0812 0.0000 15.0009 0.0000 15.0009
  3 件のコメント
Sachin Patil
Sachin Patil 2016 年 9 月 22 日
No sir, I want to process column by column not complete matrix.
KSSV
KSSV 2016 年 9 月 22 日
you may try to save your text file as .xls file or .csv file then use the xlsread() function..

サインインしてコメントする。

採用された回答

KSSV
KSSV 2016 年 9 月 22 日
編集済み: KSSV 2016 年 9 月 22 日
clc; clear all ;
for i = 1:10 % loop for each column
ColNum = i; %colmn number
fmt = [ repmat('%*s ',1,ColNum-1), ' %f %*[^\n]'] ;
fid = fopen('data.txt') ;
data = textscan(fid, fmt) ;
%%do what you want %%
data{1}
fid = fclose(fid);
end
  3 件のコメント
Sachin Patil
Sachin Patil 2016 年 9 月 22 日
Dr. Siva Srinivas Kolukula, please help.
Stephen23
Stephen23 2016 年 9 月 22 日
@Sachim Patel: this is very inefficient concept and very slow code. Reading and writing from file is a slow process. Your entire concept would be simpler and faster if you simply read the whole data into MATLAB once, and perform whatever operation you want inside MATLAB. You should also learn how to process entire matrices and arrays using MATLAB, otherwise your code will be very inefficient.
See my answer for a much more efficient solution.

サインインしてコメントする。

その他の回答 (1 件)

Stephen23
Stephen23 2016 年 9 月 22 日
This is a much simpler and more efficient code:
M = dlmread('test.txt');
for row = 1:size(M,1) % loop over the rows
M(row,:)
end
for col = 1:size(M,2) % loop over the columns
M(:,col)
end
Tested on this file:

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by