fileread txt file problem
12 ビュー (過去 30 日間)
古いコメントを表示
Anak Agung Adhi Dermawan
2022 年 6 月 25 日
コメント済み: Anak Agung Adhi Dermawan
2022 年 6 月 26 日
Greeting everyone, I want to read multiple text file and process it with my script and save it with the same name, but I got error in fileread, can anyone help me?
clc;clear all; close all;
format long g;
% Specify the folder where the files live.
myFolder = 'E:\Data\';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir(); % Ask for a new one.
if myFolder == 0
% User clicked Cancel
return;
end
end
Allfile = fullfile(myFolder, '*.txt');
List = dir(Allfile);
for k = 1 : length(List)
sites = List(k).name;
content=fileread(sites);
data=textscan(content,'%f%f%f%f[^\n\r]') ;
data=data{1};
% sum up dryz+wetz in txt file
odd_rows = data(1:2:end,3);
even_rows = data(2:2:end,3);
ztd = odd_rows + even_rows;
% standard deviation values
stdev = data(1:1:end,4);
newdata = [ztd stdev];
%save data
save([ '.txt'],'newdata','-ascii');
end
0 件のコメント
採用された回答
Cris LaPierre
2022 年 6 月 25 日
Consider looking into readtable or readmatrix instead of textscan.
% if you want column 5
format long g
file = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/1045490/VMF1.BAKO.txt';
data = readtable(file,'TextType','string')
% if you don't
num = readmatrix(file);
num(:,end)=[]
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Text Files についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!