How to convert array so that I have only numerical values

4 ビュー (過去 30 日間)
Pavan Singh
Pavan Singh 2020 年 11 月 2 日
編集済み: Stephen23 2020 年 11 月 2 日
I need to turn this array to a graph, to do so I only need numerical values

回答 (3 件)

Sriram Tadavarty
Sriram Tadavarty 2020 年 11 月 2 日
Hi Pavan,
As from the snippet provided, i can see a pattern where from 6 element the numeric value starts and has till last but before element.
Assuming temp as a variable having that data. Here is the way it can be done
numericTemp = cellfun(@(x) str2num(x(6:end-1)),temp);
Hope this helps.
Regards,
Sriram

Ameer Hamza
Ameer Hamza 2020 年 11 月 2 日
This is an alternative approach
str = {'lat: -30.963#';'lat: -30.963#';'lat: -30.963#';'lat: -30.963#'}
nums = cellfun(@(x) sscanf(x, 'lat:%f#'), str)

Stephen23
Stephen23 2020 年 11 月 2 日
編集済み: Stephen23 2020 年 11 月 2 日
Do NOT use cellfun for this, unless really you want your code to be slow.
The most efficient solution by far is this:
str = {'lat: -30.963#';'lat: -30.964#';'lat: -30.965#';'lat: -30.966#'};
vec = sscanf([str{:}],'lat:%f#')
vec = 4×1
-30.9630 -30.9640 -30.9650 -30.9660

カテゴリ

Help Center および File ExchangeDirected Graphs についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by