fileread txt file problem
10 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Anak Agung Adhi Dermawan
le 25 Juin 2022
Commenté : Anak Agung Adhi Dermawan
le 26 Juin 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 commentaires
Réponse acceptée
Cris LaPierre
le 25 Juin 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)=[]
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Text Files dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!