fileread txt file problem

7 visualizaciones (últimos 30 días)
Anak Agung Adhi Dermawan
Anak Agung Adhi Dermawan el 25 de Jun. de 2022
Comentada: Anak Agung Adhi Dermawan el 26 de Jun. de 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

Respuesta aceptada

Cris LaPierre
Cris LaPierre el 25 de Jun. de 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')
data = 578×5 table
Var1 Var2 Var3 Var4 Var5 _________ ____ _________________ ____ _________________________ 631108800 0 2.27138271815513 1 ".Station.BAKO.Trop.DryZ" 631108800 0 0.363776816230897 1 ".Station.BAKO.Trop.WetZ" 631109100 0 2.27137346598078 1 ".Station.BAKO.Trop.DryZ" 631109100 0 0.363766189129965 1 ".Station.BAKO.Trop.WetZ" 631109400 0 2.27136135737736 1 ".Station.BAKO.Trop.DryZ" 631109400 0 0.363775316942192 1 ".Station.BAKO.Trop.WetZ" 631109700 0 2.27134924877232 1 ".Station.BAKO.Trop.DryZ" 631109700 0 0.363784444755643 1 ".Station.BAKO.Trop.WetZ" 631110000 0 2.27133714016891 1 ".Station.BAKO.Trop.DryZ" 631110000 0 0.363793572567871 1 ".Station.BAKO.Trop.WetZ" 631110300 0 2.27132503156387 1 ".Station.BAKO.Trop.DryZ" 631110300 0 0.363802700381322 1 ".Station.BAKO.Trop.WetZ" 631110600 0 2.27131292296045 1 ".Station.BAKO.Trop.DryZ" 631110600 0 0.363811828193549 1 ".Station.BAKO.Trop.WetZ" 631110900 0 2.27130081435541 1 ".Station.BAKO.Trop.DryZ" 631110900 0 0.363820956007001 1 ".Station.BAKO.Trop.WetZ"
% if you don't
num = readmatrix(file);
num(:,end)=[]
num = 578×4
1.0e+00 * 631108800 0 2.27138271815513 1 631108800 0 0.363776816230897 1 631109100 0 2.27137346598078 1 631109100 0 0.363766189129965 1 631109400 0 2.27136135737736 1 631109400 0 0.363775316942192 1 631109700 0 2.27134924877232 1 631109700 0 0.363784444755643 1 631110000 0 2.27133714016891 1 631110000 0 0.363793572567871 1

Más respuestas (0)

Etiquetas

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by