Reshape matrix adding columns of zeros at specific index

5 views (last 30 days)
Hello,
I am a beginner in Matlab and right I am a bit stack in a part of my code. The code import a baseline txt. file data that contain variables from combustion engine. The baseline has A=4x178 first row name of variables and the other three numbers.
The thing is that after a time (project duration) the number of variables is increased (new variables are added) and now I have a txt files composed by B=4X204 with 26 new variables.
Without enter in detail the script to export the txt. file in the right format, what I have got is a vector of index of which new variables in B are not in A.
Now, what I am try to do is reshape A to be the same size than B but with zeros columns at the specific index.
I would be really grateful whether somenone could point me out in the right direction.
Thank you very much in advanced.
if Start_comparison==0
if columns_baseline<columns_logs
index_new=~ismember(Variables_Logs,Variables_Baseline);
Numeric_index=find(double(index_new));
end
end
  1 Comment
Guillaume
Guillaume on 22 Nov 2018
Edited: Guillaume on 22 Nov 2018
What are A and B, tables? cell arrays? something else? They can't be matrices.
Does Variables_Logs correspond to B and Variables_Baseline correspond to A? What are the actual variable names for A and B.
Note that in your code, the conversion to double (in the find) is completely unnecesarry. It would work just as well an faster if you leave index_new as a logical array. The find is probably unnecessary as well.

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 23 Nov 2018
The simplest way to do what you want is to import the files as tables and let matlab do all the work of figuring out the format and missing columns for you. readtable with the correct import option can do it all. Interestingly, testing with your files let me find some strange quirks of readtable that I'll be reporting shortly to Mathworks.
Note that your demo files have a tab character at the end of each line. That's not normal and even in excel results in an extra column of data imported. You ought to fix your export code from puma. i assume it's not written by AVL. Thankfully, we can tell matlab not to import that last empty column.
I'm not reproducing the UI part of your code, the rest can be replaced by:
%these three comes from UI
path = pwd;
Baseline_TXT = 'Baseline.txt';
Log_TXT = 'Logs.Txt'
%import code
opts = detectImportOptions(fullfile(path, Log_TXT)); %detect format of text file
opts.ExtraColumnsRule = 'ignore'; %to ignore extra tab at the end of lines. Otherwise, default rule of 'addVars' would add an extra variable
%default of opts.MissingRule is 'fill' which is exactly what we want. Missing columns will be filled by FillValue (NaN by default)
%We could tell matlab to import the 2nd line as units. However, there's a quirk where it doesn't work with missing variables. Not critical anyway
%opts.VariableUnitsLine = 2; %bug!
log = readtable(fullfile(path, Log_TXT), opts);
baseline = readtable(fullfile(path, Baseline_TXT), opts);
Since I'm using the import options of the log file for loading the baseline. It uses all the variable defined in the log file, and if not present fill them with NaN because of the default 'fill' value of MissingRule. As you can see in just 4 lines, everything is done.
  6 Comments
Santos Romero
Santos Romero on 24 Nov 2018
Edited: Santos Romero on 24 Nov 2018
Thank you very much for the function . I can tell that you do this every day. I only try when I really need to and I cannot detect if the code is robust or fragile. I am happy if the code does what I am looking for, considering that the volume of data I require to process it is not really high.
I totally agree that Matlab has improved a lot slong the years but unfortunately in the company I work excel is mainly used.
I will try the code on Monday when I get to work and let you know. I will take the oportunity to learn some matlab from your code.
I really appreciate your help again.
Santos Romero
Santos Romero on 26 Nov 2018
Good morning,
I have tried the code this morning and work perfectly, achieving what I am looking for.
I am really grateful for your advice and help as well as I have learnt from your code too!!!
Thanks again!!

Sign in to comment.

More Answers (0)

Products


Release

R2007b

Community Treasure Hunt

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

Start Hunting!