fileread txt file problem
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Anak Agung Adhi Dermawan
am 25 Jun. 2022
Kommentiert: Anak Agung Adhi Dermawan
am 26 Jun. 2022
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 Kommentare
Akzeptierte Antwort
Cris LaPierre
am 25 Jun. 2022
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)=[]
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Text Files finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!