fileread txt file problem
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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
0 comentarios
Respuesta aceptada
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')
% if you don't
num = readmatrix(file);
num(:,end)=[]
Más respuestas (0)
Ver también
Categorías
Más información sobre Text Files en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!