how to save the map information in a structure?

조회 수: 2 (최근 30일)
kuldeep
kuldeep 2024년 9월 22일
편집: Shubham 2024년 9월 22일
I am reading the header file as
header_file = 'C:\preprocessed\header.hdr';
info = enviinfo(header_file);
disp(info);
map_info = info.MapInfo;
disp(map_info);
ProjType: "UTM"
PixelTiePoints: [14186 1.0802e+04]
MapTiePoints: [4.3450e+05 2.0048e+06]
PixelSize: [10 10]
ProjZone: 43
NorthOrSouth: "North"
Datum: "World Geodetic System 1984"
Units: "Meters"
but I want to get it as follows
map info = {UTM,14186.0,10802.5,434499.57858948375,2004780.07582265,10.0,10.0,43,North,World Geodetic System 1984,units=Meters}
please suggest me how to get it as mentioned above.
kuldeep

답변 (1개)

Shubham
Shubham 2024년 9월 22일
Hey Kuldeep,
In order to convert objects into a structure, simply use the "struct" function in MATLAB: https://www.mathworks.com/help/matlab/ref/struct.html#d126e1734438
s = struct(obj)
However, based on your query, I think you wish to store the information in the form of a cell array. For converting a struct into a cell array, pass it as an input parameter to the function "struct2cell": https://www.mathworks.com/help/matlab/ref/struct2cell.html
Happy coding!
  댓글 수: 3
Umar
Umar 2024년 9월 22일

Hi @Kuldeep,

In order to achieve the desired output, you need to construct a formatted string that encapsulates various pieces of map information. Here is a breakdown of the code:

Variable Definitions

Start by defining individual variables for each piece of information we want to include in our output. This includes the UTM type, several numerical values, directional information, and the datum.

Using sprintf

The sprintf function is employed to format the string. This function allows us to create a string with specific formatting options. The placeholders within the string (e.g., %s, %.1f, %.15f, %d) are replaced by the corresponding variable values. %s is used for strings,%.1f for floating-point numbers with one decimal place,%.15f for floating-point numbers with fifteen decimal places,%d for integers.

Displaying the Output

Finally, use disp to print the formatted string to the console. This will show the output exactly as specified in the requirement. By executing this code, you will obtain the desired output, formatted precisely as requested.

Here is the refined code below which can be customized based on your later needs.

% Define the map information
utm = 'UTM';
value1 = 14186.0;
value2 = 10802.5;
value3 = 434499.57858948375;
value4 = 2004780.07582265;
value5 = 10.0;
value6 = 10.0;
value7 = 43;
direction = 'North';
datum = 'World Geodetic System 1984';
units = 'units=Meters';
% Create the formatted output
map_info = sprintf('map info =   
{%s,%.1f,%.1f,%.15f,%.15f,%.1f,%.1f,%d,%s,%s,%s}', ...
                 utm, value1, value2, value3, value4, value5,   
value6, value7, direction, datum, units);
% Display the output
disp(map_info);

Please see attached.

Please let me know if you have any further questions.

Shubham
Shubham 2024년 9월 22일
편집: Shubham 2024년 9월 22일
Hey Kuldeep,
  • For getting the desired result, you can create a custom function which extracts each property from the class. You can use the sprintf method as mentioned by Umar, but instead of using "Value1", "Value2" etc, use the actual fields, for eg: "map_info.ProjType", "map_info.PixelTiePoints", etc.
  • Otherwise the cell array that you have already constructed using "struct2cell", you can just loop over the terms and form a separate array/cell array as you need. If the values are numeric or string, append them in "map_info2"(new array), otherwise use a nested loop/do this recursively.
I would recommend the second method as it would be more generalised.

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

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by