read files from 1 dic
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi, i want to read various files from 1 past
but i have this error:
Instead its type was struct.
Error in interp (line 47)
validateattributes(idata,{'single','double'},{'vector'},'interp','x');
Error in Untitled_interpolar (line 33)
ecg_new = interp(ecg,5);
clear, close
clc
path_dados_originais = "C:\Users\35191\OneDrive - Universidade de Aveiro\Emotional transition - recognition\Dados_Sessoes\S1.mat";
path_dados_interpolados = "C:\Users\35191\OneDrive - Universidade de Aveiro\Emotional transition - recognition\Dados_Sessoes\S1_interpolados";
cd(path_dados_originais)
%Ficheiros a usar:
allfiles = dir;
Nfiles = size(allfiles(3:end,1),1);
% Transformação de .txt para .mat
% for idx = 1:length(allfiles)
% file_name = allfiles(idx).name;
%
% fprintf("Processing File %s\n",file_name);
% data = importdata(file_name);
% [filepath,name,ext] = fileparts(fullfile(pwd,file_name));
% save([name '.mat'],'data');
% end
for j=1:Nfiles
cd(path_dados_originais)
%Ler os dados
file = allfiles(j+2).name;
eval(['load ' file ])
ecg = data(:,1);
ecg_new = interp(ecg,5);
cd(path_dados_interpolados)
mat_dados = [ecg_new];
eval(['save' file(1:end-4) ' mat_dados'])
end
1 commentaire
Rik
le 7 Nov 2021
Why are you using this fragile code design with eval? Also, none of the code indicates what could be the source of the problem, so you will have to attach one of your mat files and explain what you want to happen.
Réponses (1)
Samay Sagar
le 29 Fév 2024
The error message you're encountering indicates that the “interp” function expects its first argument to be a vector of type single or double, but it received a variable of type struct. If the data loaded from your MAT file is in the form of a struct, Passing the entire struct to “interp” instead of just a numeric vector would result in this error. It will be helpful if you can share the format of the input to the “interp” function.
Additionally, there's an error with the use of the “cd” command. You are attempting to change the directory to the path of a MAT file, which is not a directory. You should change the directory to the folder containing the MAT files, not the file itself.
Follow these steps to rectify your code:
- Ensure that the “path_dados_originais” is a directory, not a file .
- Use the correct field from the “struct” data loaded from the MAT file when calling “interp”.
- Change to the correct directory before starting the loop to process files.
Here's how you might change your code to fix those errors:
clear; close all; clc;
% Define the directory paths
path_dados_originais = "C:\Users\35191\OneDrive - Universidade de Aveiro\Emotional transition - recognition\Dados_Sessoes";
path_dados_interpolados = "C:\Users\35191\OneDrive - Universidade de Aveiro\Emotional transition - recognition\Dados_Sessoes\S1_interpolados";
% Ensure the output directory exists
if ~exist(path_dados_interpolados, 'dir')
mkdir(path_dados_interpolados);
end
% Get a list of all MAT-files in the original data directory
cd(path_dados_originais);
allfiles = dir('*.mat'); % Only list .mat files
Nfiles = length(allfiles);
% Loop over each file to process
for j = 1:Nfiles
% Load the data from the .mat file
file = allfiles(j).name;
data = load(file);
% Assume 'data' is a struct and 'ecg' is a field in it
% You may need to adjust this if the field name is different
if isfield(data, 'ecg')
ecg = data.ecg;
else
error('The loaded struct does not contain an ''ecg'' field.');
end
% Validate that 'ecg' is a numeric vector before interpolation
validateattributes(ecg, {'single', 'double'}, {'vector'}, file, 'ecg');
% Interpolate the ECG data
ecg_new = interp(ecg, 5);
% Save the interpolated data to the new directory
save(fullfile(path_dados_interpolados, [file(1:end-4) '_interpolados.mat']), 'ecg_new');
end
Read more about “interp” and “cd” here:
0 commentaires
Voir également
Catégories
En savoir plus sur ECG / EKG 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!