Trouble importing data from folder

2 Ansichten (letzte 30 Tage)
Chad
Chad am 3 Sep. 2023
Beantwortet: Voss am 3 Sep. 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

Antworten (2)

Star Strider
Star Strider am 3 Sep. 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 am 3 Sep. 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) 

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by