Filter löschen
Filter löschen

Incompatible Array Sizes Error

2 Ansichten (letzte 30 Tage)
Peter Gillen
Peter Gillen am 9 Feb. 2022
Kommentiert: Peter Gillen am 9 Feb. 2022
I am attempting to write a script that allows a user to filter a list of parks by either Country, State, or City. The data is stored in an excel spreadsheet as CSV's. The script will first ask the user how they want to filter the data (Country, State, City) and, based on the user input a set of statements should execute allowing the user to input either the specific Country, State, or City and returning the parks located there. My script works if Country is initially entered. However, if State or City is entered, I get an error stating "Arrays have incompatible sizes for this operation". If I comment out the if/elseif statements for Country and City then the State input is accepted and the script works as normal. If I comment out Country and State then the City input is accepted and works as normal. However, when I leave them all uncommented, the error occurs. What am I missing?
Section 1
T=readtable('BaseballParks.csv');
columnTitle=input('Would you like to search by Country, State, or City? ','s')
columnFilter(columnTitle)
Section 2
function filtertype=columnFilter(columnTitle)
T=readtable('BaseballParks.csv');
filterselection=table2array(T(:,columnTitle));
if columnTitle=='Country'
i2=input('Please enter the abbreviation of the country to be searched: ','s');
countryChoice=ismember(filterselection,i2);
parksCountry=T(countryChoice,:)
disp(parksCountry)
elseif columnTitle=='State'
i2=input('Please enter the abbreviation of the state to be searched: ','s');
stateChoice=ismember(filterselection,i2);
parksState=T(stateChoice,:)
disp(parksState)
elseif columnTitle=='City'
i2=input('Please enter the city to be searched: ','s');
cityChoice=ismember(filterselection,i2);
parksCity=T(cityChoice,:)
disp(parksCity)
end
end

Akzeptierte Antwort

Image Analyst
Image Analyst am 9 Feb. 2022
Don't use == for comparing strings. Use strcmpi
elseif strcmpi(columnTitle, 'State')
or use contains
elseif contains(columnTitle, 'State', 'IgnoreCase', true);
Try this:
% Demo by Image Analyst
% Initialization Steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 15;
markerSize = 40;
%================================================================================
% Section 1
T = readtable('BaseballParks.csv')
columnTitle = input('Would you like to search by Country, State, or City? ','s')
T = readtable('BaseballParks.csv');
columnFilter(T, columnTitle)
%================================================================================
% Section 2
function filtertype=columnFilter(T, columnTitle)
if contains(columnTitle, 'Co','IgnoreCase',true)
tableColumn = lower(T.Country);
locationCode = lower(input('Please enter the abbreviation of the country to be searched: ','s'));
elseif contains(columnTitle, 'St','IgnoreCase',true)
tableColumn = lower(T.State);
locationCode = lower(input('Please enter the abbreviation of the state to be searched: ','s'));
elseif contains(columnTitle, 'Ci','IgnoreCase',true)
tableColumn = lower(T.City);
locationCode = lower(input('Please enter the city to be searched: ','s'));
end
% Filter the table and display results.
rows = ismember(tableColumn, locationCode);
results = T(rows,:)
% disp(results)
end
  1 Kommentar
Peter Gillen
Peter Gillen am 9 Feb. 2022
Thank you for this answer. Simply changing the == to strcmpi worked wonders. If you have the time, would you be able to explain why the error occured for an input of "State" or "City" but not for "Country"? I am currently working on this assignment for class and will stick with only modifiying the == to strcmpi in order to stay within the bounds of what we have covered but I will save the script you created and study it to understand your method and the functions you used. Your script is undoubtedly much more efficient. Cheers!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Gonzalo Martínez de Pissón
I usually use switch for cases like this. Check the code below:
function filtertype=columnFilter(columnTitle)
T=readtable('BaseballParks.csv');
filterselection=table2array(T(:,columnTitle));
switch columnTitle
case 'Country'
i2=input('Please enter the abbreviation of the country to be searched: ','s');
countryChoice=ismember(filterselection,i2);
parksCountry=T(countryChoice,:)
disp(parksCountry)
case 'State'
i2=input('Please enter the abbreviation of the state to be searched: ','s');
stateChoice=ismember(filterselection,i2);
parksState=T(stateChoice,:)
disp(parksState)
case 'City'
i2=input('Please enter the city to be searched: ','s');
cityChoice=ismember(filterselection,i2);
parksCity=T(cityChoice,:)
disp(parksCity)
end
end
  1 Kommentar
Peter Gillen
Peter Gillen am 9 Feb. 2022
Thank you for taking the time to answer my question. I was able to get the code running with the quick change of == to strcmpi given by Image Analyst but I will also be saving your code to study and learn from. I like that your script is very concise, neat, and easy for a MATLAB noob to comprehend given what I have learned thus far.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by