Matlab-Excel shape PROBLEM

Buongiorno, ho un file di testo che mi riporta una serie di dati in matrice e lo devo convertire in excel.
Il problema però non è questo, perchè lo so fare, ma il fatto è che devo mettere la matrice disposta in un determinato modo.
Ad esempio le primo 19 colonne le devo mettere tutte sulla stessa riga, e così dalla 20 alla 29 colonna ecc..
La mia matrice ha più di 1000 colonne!
Come posso fare?
Goodmorning I have a text file that gives me a serie of datas in matrix and I have to convert it in excel.
But that's not the problem, I can do that easily!
THe problem is that I have to shape the matrix in a different way, infact I have to take the first 19 columns and put them on the same row. And I have to do that with the columns from 20 to 29.
I have more than 1000 columns, how can I do that? Please!!

5 Comments

So the first row should have 19 columns and the second row should have 10 columns? How many columns should the other rows have?
MATLAB arrays cannot have a different number of columns but if you put NaN in a location it show up empty in excel
Could be a typo, which is what I assumed (I know I should not have).
Nonetheless, OP has not replied yet given the "urgency" of their question.
Ok no I haven't been clear and it's really difficult to explain. Basically I have some datas in a certain number of columns and of rows and I have to took them in Excel. The problem is that I have to reshape the datas in order to have for example the numbers from the 1st to the 20th position in the first column and move them in the first row. Then I have to do the same thing with the numbers from the 21th to the 30th with the second row and so on. I don't know if it's clear. I know that you cannot change the shape of the matrix that's why I have problems.
I am taking the datas from a .txt Also I was thinking of using a for function. I don't know. I'm welcome to any advise. Thank you for the help!
What is the format of the txt file?

Sign in to comment.

Answers (1)

Create a vector containing the number of consecutive elements needed to move into each row. Not the indices, the count. So for example counts = [19 10 ... whatever] then
largest_split = max(counts);
total_wanted = sum(counts);
Column_to_Extract = 1;
splits = mat2cell(YourData(1:totalwanted,Column_to_Extract), counts, 1);
Block = cell2mat(cellfun(@(V) [reshape(V, 1, []), nan(1, largest_split-numel(V))], splits(:), 'uniform', 0));
Now if I did everything properly, Block should end up as a numeric array in which groups of consecutive rows in YourData have been converted into one row per group, and each row will be padded out with NaN to the length of the longest row.
If you now write that data out to an excel file, such as with writematrix() then the NaN will be converted to empty cells.

Products

Release

R2023a

Asked:

on 30 Oct 2023

Commented:

on 31 Oct 2023

Community Treasure Hunt

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

Start Hunting!