MATLAB: How to extract some rows in a marix that satisfy a given condition

datamatrix manipulationtext file

Hi everyone here my data:
  • a file text with the name of nodes (of a network) for example ('JUNCTION-0;JUNCTION1…') ordered by columns
  • a file.mat that contains the demands of nodes
First of all I want to group the two files in a matrix/data table and then extract only the rows for which the demand is >0. The final matrix should contain only the names of junctions with the corresponding demand that satisfy the given condition. Any suggestions? I attach the two files.
Thank you!
Stefania

Best Answer

Hello,
clc;
clear;
Nodi = Read_File('nodi.txt');
load demand.mat
basedemand = basedemand'; % column orientation
basedemand(end+1:length(Nodi),1) = nan; % your data does not have equal num
% of rows, filling with nans
idx = basedemand>0;
Nodes = Nodi(idx);
Demand = basedemand(idx);
T = table(Nodes,Demand)
function File_Data = Read_File(Input_File_Path)
Temp_File_Data = fileread(Input_File_Path);
File_Data (:,1) = strsplit(Temp_File_Data, newline)'; % Column-wise file orientation
end
Your data does not have equal number of rows, some information might be missing, you should check the files.