Incompatible Array Sizes Error
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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
0 Kommentare
Akzeptierte Antwort
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
Weitere Antworten (1)
Gonzalo Martínez de Pissón
am 9 Feb. 2022
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
Siehe auch
Kategorien
Mehr zu System Composer finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!