I have two files, one in excel and one in a text file, how would i obtain column data from the text file even though i did it from an excel file.
code is:
filename = 'mct.xlsx'
data = xlsread(filename, 'mct', 'A:D');
Phase1 = data(data(:4)==1,:);
how would i be able to extract this from a text file?

 채택된 답변

fred  ssemwogerere
fred ssemwogerere 2020년 2월 3일

0 개 추천

Hello, there are a number of ways to go about this. Please refer to the following links:

댓글 수: 1

isamh
isamh 2020년 2월 3일
편집: isamh 2020년 2월 3일
saw this example and had several questions
x = 100*rand(8,1);
fileID = fopen('nums1.txt','w');
fprintf(fileID,'%4.4f\n',x);
fclose(fileID);
what does the 'w' mean?
also, what does ''%4.4f\n'' mean?

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

fred  ssemwogerere
fred ssemwogerere 2020년 2월 4일

0 개 추천

% what does the 'w' mean?
'w' gives write access permission to the file
% also, what does ''%4.4f\n'' mean?
This is a formatting operator that prints each input as a fixed point number having field width of 4, and the number of digits after the decimal point of 4, before proceeding to a new line ("\n").

댓글 수: 8

this is the code i used,
for x, what if i dont know the # of rows? # of columns will be 4 for sure but how would i make it read the # of rows automatically.
x=rand(4,30000);
fileID = fopen('MCT_Drive_Traces.txt','w');
fprintf(fileID,'%4.4f\n',x);%adjust according to # of columns
fclose(fileID);
T = .1;
these are the error messages that i get:
1) Index in position 2 exceeds
array bounds (must not
exceed 1).
2) Error in Untitled (line 20)
Phase_1 =
fileID(fileID(:,4)==1,:);
thanks again for the help!
dlmwrite('MCT_Drive_Traces.txt', x, 'delimiter', ' ', 'precision', 4)
You might possible prefer 'delimiter', '\t' for tabs between columns.
thanks!
i got this error:
Error using dlmwrite (line
104)
\t is not a valid
attribute or delimiter.
Delimiter must be a single
character.
code is:
dlmwrite('MCT_Drive_Traces.txt', x, 'delimiter', ' \t', 'precision', 4)
%% Phase 1
Phase_1 = dlmwrite(dlmwrite(:,4)==1,:);
for phase_1, i want to obtain only 1 on the fourth column including the rows associated with that column
You have space tab for delimiter; it looks like you need '\t'
Phase_1 = dlmwrite(dlmwrite(:,4)==1,:);
??? You are trying to call the function dlmwrite() passing in a colon as the first parameter ??
dlmwrite('Phase_1.txt', x(x(:,4)==1,:), 'delimiter', 't', 'precision', 4)
isamh
isamh 2020년 2월 5일
편집: isamh 2020년 2월 5일
thanks for everything! got it to work
fid = fopen('MCT_Drive_Traces1.txt', 'rt');
DATA = cell2mat( textscan(fid, '%f%f%f%f', 'Delimiter', 't', 'HeaderLines', 4) );
fclose(fid);
%% Phase 1
Phase_1 = DATA(DATA(:,4)==1,:);
Walter Roberson
Walter Roberson 2020년 2월 5일
delimiter '\t' not 't'
isamh
isamh 2020년 2월 5일
편집: isamh 2020년 2월 5일
fid = fopen('MCT_Drive_Traces1.txt', 'rt');
DATA = cell2mat( textscan(fid, '%f%f%f%f', 'Delimiter', '\t', 'HeaderLines', 4) );
fclose(fid);
%% Phase 1
Phase_1 = DATA(DATA(:,4)==1,:);
hey, i tried the code above and noticed that the matrix DATA doesn't contain all the rows it's missing about one fifth of the rows.
Fixed the problem, there were headers between the numbers at some certian rows, would there be a way to ignore all headers except for the first row?
Walter Roberson
Walter Roberson 2020년 2월 5일
Sometimes you can use textscan CommentStyle to ignore headers if they always have the same start and stop. Otherwise sometimes you end up looping textscan, especially if you want the blocks to be separated in output.
Another method that can be very useful is to fileread() the entire file as a character vector, and then use a combination of regexp and regexprep to extract parts of it, possibly then passing the resulting characters into textscan.

댓글을 달려면 로그인하십시오.

isamh
isamh 2020년 2월 5일

0 개 추천

will try that out, thanks for everything Walter!
isamh
isamh 2020년 2월 12일
편집: isamh 2020년 2월 12일

0 개 추천

Hey Walter, I tried to ignore any text that appears within the TXT file but kept getting an error message.
Code:
result = [];
fid=fopen('MCT_Data.txt');
tic
while 1
tline = fgetl(fid);
if ~ischar(tline), break, end
celldata = textscan(tline,'%f %f %f %f %f %f');
matdata = cell2mat(celldata);
% match fails for text lines, textscan returns empty cells
result = [result ; matdata];
end
toc
fclose(fid);
the error message is:
Error using textscan.
First input can not be empty. Expected a non-empty character vector or a valid file-id.
so, the two rows contain
R E V D
m k h -
435 345 435 3543
...
the file has alot of numerical data. the headers repeat randomly and are the same every time they repeat.
how would ignore everything but numerical data?

댓글 수: 3

fgetl returns something that is not a character when it reaches end of file.
fgetl can return empty if the only thing on a line is the line terminator.
if isempty(tline); continue; end
isamh
isamh 2020년 2월 14일
would this: if isempty(tline); continue; end be placed after results? or
got this code from another question and it works but takes some time.
str = fileread('MCT_Data.txt');
nums = cellfun(@str2double, regexp(str, '([\d.,]+)', 'match'));
x = reshape(nums, 4, [])';
result = [];
fid=fopen('MCT_Data.txt');
tic
while 1
tline = fgetl(fid);
if ~ischar(tline); break; end %end of file
if isempty(tline); continue; end %empty line
celldata = textscan(tline,'%f %f %f %f %f %f');
matdata = cell2mat(celldata);
% match fails for text lines, textscan returns empty cells
result = [result ; matdata];
end
toc
fclose(fid);
What is your file format? It appears that you have text at the beginning of some of the lines, and you are doing this looping so that you can ignore those lines? There are usually easier ways to deal with such files.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

질문:

2020년 2월 3일

댓글:

2020년 2월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by