Bubble sorting (Index exceeds the number of array elements)

I'm supposed to write a program that bubblesorts surnames. Porg file contains surnames while Klucze file contains keys assigned to surnames.
global Staff
global keys
fid = fopen ('Porg.csv')
fid2 = fopen ('Klucze.csv')
i=2
while ~feof(fid); ~feof(fid2)
sd=fgetl(fid);
zd=fgetl(fid2)
tet(1:3)=strsplit(sd,';');
zet(1:3)=strsplit(zd,' ');
Staff(i).surname=tet(1);
Staff(i).name=tet(2);
keys(i).key=zet(1);
i=i+1;
end
for x=2:(i-1)
v(1)=Staff(x).surname;
v(2)=Staff(x).name;
v(3)=keys(x).key;
disp(v);
end
This code displays surnames together with names and assigned keys. Tried to bubble sort it by changing the last loop, and I keep getting an error "Index exceeds the number of array elements (1)". Does anyone know how to fix it? Probably it's a rookie mistake (I'm a newbie when it comes to programming)
g = length('Porg.csv')
for j=2:(g-1);
if j(g)>j(g+1);
temp=Staff(j).surname;
Staff(j).surname = Staff(j+1).surname;
Staff(j+1).surname = temp
end
v(2)=Staff(h).name;
v(3)=keys(h).key;
disp(v);
end

3 Commenti

Upload porg.csv
What do you expect this to do?:
g = length('Porg.csv')
Please upload some sample data files by clicking the paperclip button.
Well, I was hoping for Matlab to read 36 columns from Porg file as one variable, but I guess that's not the solution. As I said, I'm new to programming so I don't know which function should I use to get the results I want. Added files, changed words in the first file cause the original one contains personal data.

Accedi per commentare.

 Risposta accettata

Hi Karolina,
I always use xlsread instead of fopen. It is always easier to see what's going on with cell arrays or table structs.
Please try below code:
[~, ~ , Porg] = xlsread('Porg.csv'); % gets raw Porg data
[~, ~ , Klucze] = xlsread('Klucze.csv'); % gets raw Klucze data
mergedTable = horzcat(Porg,Klucze); % merges two tables together horizontally
sortedTable = sortrows(mergedTable,1); % sorts by first column (surname)
newProg = sortedTable(:,1:3); % seperates first 3 column into new Prog data
newKlucze = sortedTable(:,4); % seperates last column into new Klucze data
xlswrite('newPorg.csv',newPorg); % saves your new cell array into a csv file again.
xlswrite('newKlucze.csv',newKlucze);

4 Commenti

According to your error message: 'Index exceeds the number of array elements (1)' , you might get that because in the Porg file you have a header like "name", "surname", etc...
That means you have 35 rows people data, the first one is the header. In Klucze data you have 36 rows. Please check that.
Thank you so much. The code above displays surnames together with numbers. I've got an error while trying to sort it (It assings a number to header, but I don't mind it) May I ask a question regarding you code? It creates new files with the same amount of columns and rows. I changed the 'fid=fopen" from old files to new files in the first code, but I've got another error. "Unable to perform assignment because the left and right sides have a different number of elements." in Line 12 zet(1:2)=strsplit(zd,';'). Sorry for troubling you.
I think I misspelled the word newPorg to newProg above. Sorry.
Here is the code and also it creates struct arrays as you are expecting. You can comment out Staff(k).weight line as you wish.
[~, ~ , Porg] = xlsread('Porg.csv'); % gets raw Porg data
[~, ~ , Klucze] = xlsread('Klucze.csv'); % gets raw Klucze data
mergedTable = horzcat(Porg,Klucze); % merges two tables together horizontally
sortedTable = sortrows(mergedTable,1); % sorts by first column (surname)
newPorg = sortedTable(:,1:3); % seperates first 3 column into new Prog data
newKlucze = sortedTable(:,4); % seperates last column into new Klucze data
xlswrite('newPorg.csv',newPorg); % saves your new cell array into a csv file again.
xlswrite('newKlucze.csv',newKlucze);
%%Creating Struct Array for Staff and the Keys with sorted table
for k = 1:size(sortedTable,1)
Staff(k).surname = sortedTable{k,1};
Staff(k).name = sortedTable{k,2};
Staff(k).weight = sortedTable{k,3}; % I don't know what you are using this 3rd column for. There are some numbers but they are not the keys
keys(k).key = sortedTable{k,4};
end
According to your error message, zet(1:2)=strsplit(zd,';'). It does not give me an error on this line. Please try again with clearing your workspace.
g = length('Porg.csv') -> this line gives you g = 8. It is because Porg.csv is 8 characters. I did not understand why you want to use this for.

Accedi per commentare.

Più risposte (0)

Categorie

Prodotti

Release

R2018b

Richiesto:

il 9 Nov 2018

Commentato:

il 11 Nov 2018

Community Treasure Hunt

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

Start Hunting!

Translated by