Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5932891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:53:51+00:00 2026-05-22T14:53:51+00:00

Firstly, I have the user input their own text files consisting of states, capitals,

  • 0

Firstly, I have the user input their own text files consisting of states, capitals, and populations and I put all of these values into a structure array using the following code:

clear
clc
%Part A
textfile=input('What is the name of your text file?\n','s');
fid=fopen(textfile);
file=textscan(fid,'%s %s %f','delimiter',',');
State=file{1}
Capital=file{2}
Population=file{3}
regions=struct('State',State,...
    'Capital',Capital,...
    'Population',Population)
fclose(fid);

My first question: is it possible to display all of the values in the structure? Displaying the structure array just gives me this:

50x1 struct array with fields:

    State
    Capital
    Population

And my second question: is it possible for me to access information in this structure by trying to find, for example, 'California' only?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T14:53:52+00:00Added an answer on May 22, 2026 at 2:53 pm

    As you’ve already discovered, the default display of structure arrays in MATLAB doesn’t tell you much, just the array dimensions and field names. If you want to see the contents, you’ll have to create formatted output yourself. One way you can do this is to use STRUCT2CELL to collect the structure contents in a cell array, then use FPRINTF to display the cell contents in a particular format. Here’s an example:

    >> regions = struct('State',{'New York'; 'Ohio'; 'North Carolina'},...
                        'Capital',{'Albany'; 'Columbus'; 'Raleigh'},...
                        'Population',{97856; 787033; 403892});  %# Sample structure
    >> cellData = struct2cell(regions);         %# A 3-by-3 cell array
    >> fprintf('%15s (%s): %d\n',cellData{:});  %# Print the data
           New York (Albany): 97856
               Ohio (Columbus): 787033
     North Carolina (Raleigh): 403892
    

    With regard to your second question, you can collect the entries from the 'State' fields in a cell array, compare them to a given name with STRCMP to get a logical index, then get the corresponding structure array element:

    >> stateNames = {regions.State};            %# A 1-by-3 cell array of names
    >> stateIndex = strcmp(stateNames,'Ohio');  %# Find the index for `Ohio`
    >> stateData = regions(stateIndex)          %# Get the array element for `Ohio`
    
    stateData = 
    
             State: 'Ohio'
           Capital: 'Columbus'
        Population: 787033
    

    NOTE:

    As you mention in a comment, each 'Population' entry in your structure array ends up containing the entire 50-by-1 vector of population data. This is likely due to the fact that file{3} in your sample code contains a vector, while file{1} and file{2} contain cell arrays. In order to properly distribute the contents of the vector in file{3} across the elements of the structure array, you need to break the vector up and place each value in a separate cell of a cell array using NUM2CELL before passing it to STRUCT. Defining Population like this should solve the problem:

    Population = num2cell(file{3});
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

firstly i have searched a lot and all topics seems to be C# :
Firstly I hate IE but we all have to support it! I have a
I have a form input field that accepts multiple tags from a user, a
I'm developing some application using UIKit elements and xib files. Firstly I have created
I have a big problem. Firstly, I have a list of items.When user clicks
I have list of checkboxes that has all checkbox pre-checked when page load. Firstly,
I have 3 questions on the topic of batch files. Firstly I want to
I have a database table which has three relevant fields, firstly a user id,
Firstly, sorry about the wall of text. I have the following schema. Note that
I'm looking for a solution that could process large blocks of user input text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.