apply string math to everything in a table
4 ビュー (過去 30 日間)
古いコメントを表示
Budding MATLAB Jockey
2020 年 5 月 13 日
コメント済み: Budding MATLAB Jockey
2020 年 5 月 21 日
I have a table where one variable is a char array.
I am trying to scrub excess information out of that array
e.g. ' "h t t p s : / / c a . s t y l e . y a h o o . c o m / 5 - a p p s " '
I will reduce to ' "c a . s t y l e . y a h o o . c o m " '
In general I know I can do that with erase to remove the "h t t p s : / / " and then use strfind to find the next '/' and make a new variable that contains the string up to that address.
But strfind and erase don't seem to happily comply with the table format. Nor can I figure out how to apply strfind to the whole table (as opposed to writing a for loop to step through it).
Is there some way to make these functions work with tables?
0 件のコメント
採用された回答
Walter Roberson
2020 年 5 月 13 日
scrubbed = regexprep(YourTable.VariableName, {'^([^/]*/){2}\s*', '\s*/.*$'}, {'',''}, 'once', 'lineanchors', 'dotexceptnewline');
This code does not remove the spaces within the url. Doing that would certainly be possible:
scrubbed = regexprep(YourTable.VariableName, {'^([^/]*/){2}\s*', '\s*/.*$', '\s+'}, {'','',''}, 'lineanchors', 'dotexceptnewline');
10 件のコメント
Walter Roberson
2020 年 5 月 15 日
target = 'uploadable.csv';
opts = detectImportOptions(target, 'encoding', 'utf16le');
t = readtable(target, opts);
Before R2020a you will get warnings about the encoding not being supported, and also a warning about a byte order mark.
warning('off', 'MATLAB:iofun:UnsupportedEncoding')
will get rid of the message about unsupported encoding.
Or you could use
target = 'uploadable.csv';
fmt = ['"%f" "%f" "%f" "%f" "%f" %q %q "%f" "%f" ',repmat('%q ',1,10), '"%f" "%f" %q "%f"'];
fid = fopen(target, 'rt', 'n', 'utf16-le'); %ignore warning about UTF16-LE not being supported
data = textscan(fid, fmt, 'delimiter', '\t', 'headerlines', 1);
fclose(fid)
This will give you a single warning about the encoding not being supported.
If the warning about encoding really bugs you then,
target = 'uploadable.csv';
fmt = ['"%f" "%f" "%f" "%f" "%f" %q %q "%f" "%f" ',repmat('%q ',1,10), '"%f" "%f" %q "%f"'];
fid = fopen(target, 'r');
bytes = fread(fid, [1 inf], '*uint8');
fclose(fid)
s = native2unicode(bytes, 'utf16le');
data = textscan(s, fmt, 'delimiter', '\t', 'headerlines', 1);
.. provided that the files do not occupy more than about 1/3 of your available memory.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で String Parsing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!