How do I prevent imported data being overwriten generated within a loop?

8 visualizaciones (últimos 30 días)
Hi all, hope you're all well today. I am trying to prevent the last file from getting overwritten as it runs through the loop, because I want to be able to import all the files from that folder without them getting overwritten. 'theFiles' save in a 68x1 struct which contains all the filenames, FYI. My question is, how can I stop the files from being overwritten so I can use the data inside the struct? I apologies if it does not make sense, let me know & and i'll try to clarify it again. Thank you.
clc
close all
clear all
myFolder = 'C:\Users\MAR818\Desktop\Moving Vehicle_Weight_Identificaton\Combined Data';
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist, please double check file name/folder location:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
% Get a list all the files in the folder with the desired file name
% pattern.
filePattern =fullfile(myFolder,'*.txt'); % this is changeable to whatever pattern is required.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder,baseFileName);
fprintf(1, 'now reading %s\n', fullFileName);
fileID = importdata(fullFileName);
end
%Problem using this code, every time it loops the last file gets
%overwritten with the newly imported data. Find a way to solve this issue.

Respuestas (1)

KSSV
KSSV el 4 de Abr. de 2017
filePattern =fullfile(myFolder,'*.txt'); % this is changeable to whatever pattern is required.
theFiles = dir(filePattern);
fileData = cell(length(theFiles),1)
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder,baseFileName);
fprintf(1, 'now reading %s\n', fullFileName);
fileData{k} = importdata(fullFileName);
end
Note that, your files are not over written. Files are as such. When you are importing data (fileID) is getting overwritten. It is a variable in work space. You have to save them for each file. I have modified your lines. Please check. fileID is renamed to fileData. It is a cell, you can access the data using fileData{1}, fileData{2} etc,.
  5 comentarios
KSSV
KSSV el 4 de Abr. de 2017
For graphing, you need to to load all the files data and waste memory. You have to follow something like below:
filePattern =fullfile(myFolder,'*.txt'); % this is changeable to whatever pattern is required.
theFiles = dir(filePattern);
figure
hold on
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder,baseFileName);
fprintf(1, 'now reading %s\n', fullFileName);
fileData = importdata(fullFileName);
% plot your data
end
% save the figure
fabian Maritato
fabian Maritato el 4 de Abr. de 2017
I found the error before why I couldn't get any data to open from the struct, was because the files had messed up. The first code works! Just need to find a way to figure out with file belongs to which data set!
and thank you mate, appreciate it.

Iniciar sesión para comentar.

Categorías

Más información sobre Low-Level File I/O 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!

Translated by