Create comma-separated list from data of UITable

조회 수: 2 (최근 30일)
Beth Carreon
Beth Carreon 2022년 5월 14일
댓글: Beth Carreon 2022년 5월 21일
I want to create a list of latitude just like the format below from a UITable
latitudes = [lat1, lat2, lat3,...];
This is the data from the UITable.
I am getting this result using the following code, clearly with no commas.
m = horzcat(app.UITable.Data{:,3})
m =
'13.9524380813.9524398613.95226986 13.9475104713.94693600'
Does anyone know how to do this? Thanks
  댓글 수: 3
Beth Carreon
Beth Carreon 2022년 5월 14일
편집: Beth Carreon 2022년 5월 14일
Hello @dpb, I have tried your suggestion for the question but I'm getting error messages.
I am developing a program where I can generate a kml file using the extracted gps info (latitudes, longitudes) from the images. The gps info is stored in the UITable (third and fourth column as shown in the picture above) and I would like to extract those values. I am not sure of its data type (newbie here). Not all images have the gps info that I want thus the spaces in the column.
I want a list of the latitudes and longitudes just like the format below.
latitudes = [13.95243808, 13.95243986, 13.95226986];
longitudes = [121.65762189, 121.65762847, 121.65714711];
names = {'1', '2', '3'};
filename = 'test.kml';
kmlwritepoint(filename, latitudes, longitudes, 'Name', names);
The following is the code that I am working rn
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
OrigImage = imread(fullFileName);
info = imfinfo(fullFileName);
info.GPSInfo;
latitude = info.GPSInfo.GPSLatitude;
longitude = info.GPSInfo.GPSLongitude;
lat = dms2degrees(latitude);
lon = dms2degrees(longitude);
if (row >= 1) & (col >= 1)
app.DetectionEditField.Value ='Detected';
app.LatitudeEditField.Value = sprintf('%0.8f', lat);
app.LongitudeEditField.Value = sprintf('%0.8f', lon);
else
app.DetectionEditField.Value = 'Not Detected';
app.LatitudeEditField.Value = ' ';
app.LongitudeEditField.Value = ' ';
end
app.UITable.Data{k,1}=baseFileName;
app.UITable.Data{k,2}=app.DetectionEditField.Value;
app.UITable.Data{k,3}=app.LatitudeEditField.Value;
app.UITable.Data{k,4}=app.LongitudeEditField.Value;
k = app.UITable.Data{:,3};
l = app.UITable.Data{:,4};
kmlwritepoint('test.kml', k, l)
Error using kmlwritepoint (line 271)
Expected latitude coordinates to be one of these types:
single, double
I'm getting this error message.
I hope you could help me, thanks a lot.
dpb
dpb 2022년 5월 14일
See the Answer...as expected, this can be simplified greatly by proper use of data types and storage.

댓글을 달려면 로그인하십시오.

채택된 답변

dpb
dpb 2022년 5월 14일
편집: dpb 2022년 5월 14일
>> which -all kmlwritepoint
'kmlwritepoint' not found.
>>
So this is not a MATLAB function I've got... kmlwritepoint shows it in the Mapping TB so that 'splains that.
But that very same document shows (and the error reinforces) that the input it expects is just the numeric (double) vector array.
The suggestion to set the data columns to be numeric to ensure they are and then simply
names = {'1', '2', '3'};
filename = 'test.kml';
kmlwritepoint(filename, app.UITable.Data{:,2}, app.UITable.Data{:,3}, 'Name', names);
wlll be the expected syntax.
BUT -- If you would store your data in a MATLAB table and use uitable to display it to the user in the GUI, then you could retrieve the data from it directly as the table and even use the column headers -- here's an example taken from the above doc link -- I just renamed the two variables to match yours for to have some data.
Try this for grins...
t = readtable('patients.xls'); % the uitable example dataset
vars = {'Systolic','Diastolic'}; % we just want two variables
tCoords=t(1:10,vars); % and only need a few to illustrate
tCoords.Properties.VariableNames={'Latitude','Longitude'}; % pretend they're coordinates instead...
hUF=uifigure; % make a uifigure
hUIT=uitable(hUF,'Data',tCoords); % display the table
That created the following plain vanilla figure...
Now play with that some at command line...
>> hUIT.Data
ans =
10×2 table
Latitude Longitude
________ _________
124.00 93.00
109.00 77.00
125.00 83.00
117.00 75.00
122.00 80.00
121.00 70.00
130.00 88.00
115.00 82.00
115.00 78.00
118.00 86.00
>>
Observe we got the table back as a table -- much more convenient than having to deal with the string representation...
With this you can also write
>> hUIT.Data.Latitude
ans =
124.00
109.00
125.00
117.00
122.00
121.00
130.00
115.00
115.00
118.00
>>
so your call to write the data out can be reduced to
names = {'1', '2', '3'};
filename = 'test.kml';
kmlwritepoint(filename, hUIT.Data.Latitude,hUIT.Data.Longitude, 'Name', names);
which is "much more simpler" and legible to read the code, besides.
If you're not using a table to store your data in the app, you should be! :)
NB: I don't know what the three Names are supposed to match up to here; I'm guessing that may have something to do with there being the first unshown column in the table. Would need more info to know just what to do there, not having any experience with the Mapping TB and hence "knowing nuthink!" about just what kmlwritepoint is doing.
  댓글 수: 8
dpb
dpb 2022년 5월 14일
편집: dpb 2022년 5월 14일
Would have to see how you created the table -- what this is saying is that you tried to reference a table variable 'Latitude' before that particular name was put into the table -- but you don't show where/how the table was created.
What I showed above was using the data from the iminfo structure to build a table -- what variable names will be in that table will be dependent upon what are the field names in that stuct -- and we/I don't have any of your images to load to know what those are -- and in going back and looking, I overlooked that the GPS coordinates are actual substructures under GPSInfo under "info" -- hence the struct2array doesn't work as intended here, it built a table ok, but that table entry is still a struct, not the end data.
>> info.GPSInfo.GPSLatitude=randi(125,1,1)+rand()
info =
struct with fields:
GPSInfo: [1×1 struct]
>> info.GPSInfo.GPSLatitude
ans =
82.1711866878116
>> struct2table(info,'AsArray',1)
ans =
table
GPSInfo
____________
[1×1 struct]
>>
shows what happened with just a made up struct here of the same field structure.
So, that would, indeed, create the error; 'Latitude' definitely is not going to be one of the table names directly.
I tried to be too clever -- probably what have to do is to build the table from the actual field names -- although if those can change from file to file, may want to use dynamic fieldnames instead of hardcoded.
But, let's see -- if go back above and look at what you loaded...
...
latitude = info.GPSInfo.GPSLatitude;
longitude = info.GPSInfo.GPSLongitude;
lat = dms2degrees(latitude);
lon = dms2degrees(longitude);
then
tGPS=[];
vars={'GPSLatitude','GPS.Longitude'};
for k = 1:numel(theFiles) % length is somewhat flaky; best to avoid
FFName=fullfile(theFiles(k).folder,theFiles(k).name); % do need temporary
OrigImage =imread(FFName);
info = imfinfo(FFName);
tGPS=[tGPS; ...
table(string(theFiles(k).name) ...
dms2degrees(info.GPSInfo.GPSLatitude), ...
dms2degrees(info.GPSInfo.GPSLongitude), ...
'VariableNames',{'File','Latitude','Longitude'})];
end
should give you the beginning table with the three columns; the fourth about "discovered" could be built at same time if knew which field to inspect...I'd set the missing values to NaN instead of empty strings or zeros; those will show up as the "NaN" string in the table when use the format string.
It would be helpful to have a couple of the image files (use the paperclip icon) so can see what actually is the content directly.
Beth Carreon
Beth Carreon 2022년 5월 21일
Hello @dpb, I was able to finish my project because of your help, thank you very much!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by