Borrar filtros
Borrar filtros

Trouble importing data from folder

4 visualizaciones (últimos 30 días)
Chad
Chad el 3 de Sept. de 2023
Respondida: Voss el 3 de Sept. de 2023
Two part question really sorry its probably pretty simple Im new to this language
A. I am was trying to use point on a folder path then sort the files by name so the highest one is the one I import. (Incidents 30.csv for example)
i tried sth along the lines of
data_folder= some path;
files = ls(data_folder);
index_wanted = regexp(files , "Incidents*) <==== here it crashes and I dont know why????
error is STRING input must be either a char or row vector..... Which is weird because I thought Matlab would perform regexp over each row in the array. It seems to be seeing the entire length of the longest string as column length instead of simply being 1 column and dont know what to do about that.
B I tried making an table and processing it.
temp= table(files) ;
file_check= @(x ) regexp( x , "Incident*" );
rowfun( @file_check , temp)
getting an undefined function "file_check" for input arguements of type 'char'
just as a check to make sure I was simply using the regexp wrong I tried
bs = @(x) x+2;
index = table( 1:10);
rowfun(@bs , index,"OutputVariableNames" , 'out')
getting the same undefined input var error ??????
Thanks for any help

Respuestas (2)

Star Strider
Star Strider el 3 de Sept. de 2023
I’m not certain that I understand what you want to do.
If you want the number from the file name, perhaps something like this —
filename = 'Incidents 30.csv';
numberc = regexp(filename, '\d*', 'match')
numberc = 1×1 cell array
{'30'}
number = str2double(numberc)
number = 30
If your objective is something else, please describe it.
.

Voss
Voss el 3 de Sept. de 2023

ls returns a 2d character array in general (on Windows), but regexp expects a character row vector. You can covert the output of ls to a cell array in order to use it in regexp. Also, you can use things like "Incidents*" in the ls call directly in order to narrow down the file names returned.

files = ls(fullfile(data_folder,"Incidents*.csv"));
files = strtrim(cellstr(files));
index_wanted = regexp(files, "\d+", 'match', 'once');

However, dir may be easier to use than ls

files = dir(fullfile(data_folder,"Incidents*.csv"));
index_wanted = regexp({files.name}, "\d+", 'match', 'once');

By the way, the 'undefined function' errors you got when testing your anonymous functions are due to the use of @ when using the function. You only need @ when defining the function, not when using it, e.g.:

temp= table(files) ;
file_check= @(x ) regexp(  x , "Incident*" );
rowfun( file_check , temp) 

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by