Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the combination of structure and number with the same field in MATLAB

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to merge arrays of structures with the same fields in MATLAB. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

The problem encountered: I used the dir function to get the jpg,png,bmp image files under the specified path, as follows:

Pic_bmp = dir ([pName'* .bmp'])

Pic_jpg = dir ([pName'* .jpg'])

Pic_png = dir ([pName'* .png'])

(attached: about the dir function: first of all, it means directory, that is, to get all the files under the current directory. Using a = dir (pwd), you can return all the files and folders under the current path and save them to a. The returned an is a structure: a has five fields, each of which contains 13 Value.

K > > a = dir (pwd)

A =

13x1 struct arraywith fields:

Name

Date

Bytes

Isdir

Datenum

We want to get the name of a, and if we use names=a.name, we only get the last value in name, because this return mechanism is similar to the function return mechanism. A.name has to return 13 values, so it requires 13 variables to receive it, which is very troublesome. We can use the struct2cell function to convert an into an array of cells: a = struct2cell (a); the new an is a cell of 5x13 whose five lines of values are the values contained in the above five fields. So now it's easy to get all the name values: names= a (1dm:)

* stands for wildcards, and * .jpg represents all files with the extension jpg; similarly, name.* represents all files with the name name. )

Pic_bmp,pic_jpg,pic_png, all the field names of these three structural variables are the same. What should we do if we need to merge their data? There is more than one method, the most direct is allPic = [pic_bmp; pic_jpg; pic_png]; connect the three with a semicolon, and then use struct2cell to convert them into an array of cells: allPic = struct2cell (allPic), and then allPicName = allPic (1Magne:); you get all the name. So let's talk about something else: you can use struct2cell three times to convert pic_bmp,pic_jpg,pic_png to cell, then take out the name values of their first row, and merge them again.

This is the end of this article on "how to merge structures with the same fields in MATLAB". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report