码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • MATLAB解析和保存ini文件


    1. 将ini文件转换成struct结构体

    function data = ini2struct(filename)
        fid = fopen(filename, 'r');
    
        if fid == -1
            error('Unable to open file %s.', filename);
        end
    
        data = struct();
        section = '';
    
        while ~feof(fid)
            line = fgetl(fid);
            line = strtrim(line);
    
            % 如果是注释行或者空行,则继续下一次循环
            if isempty(line) || line(1) == ';' || line(1) == '#'
                continue;
            end
    
            % 如果是节标题
            if line(1) == '[' && line(end) == ']'
                section = line(2:end-1);
                data.(section) = struct();
                continue;
            end
    
            % 解析键值对
            equalIndex = strfind(line, '=');
            if isempty(equalIndex)
                error('Invalid format in file %s.', filename);
            end
    
            key = strtrim(line(1:equalIndex-1));
            value = strtrim(line(equalIndex+1:end));
            data.(section).(key) = value;
        end
    
        fclose(fid);
    end
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    2. 将结构体保存为ini文件

    function struct2ini(data, filename)
        fid = fopen(filename, 'w');
    
        if fid == -1
            error('Unable to open file %s.', filename);
        end
    
        fields = fieldnames(data);
    
        for i = 1:numel(fields)
            section = fields{i};
            keys = fieldnames(data.(section));
    
            fprintf(fid, '[%s]\n', section);
    
            for j = 1:numel(keys)
                key = keys{j};
                value = data.(section).(key);
                fprintf(fid, '%s=%s\n', key, value);
            end
    
            fprintf(fid, '\n');
        end
    
        fclose(fid);
    end
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    3. 读取ini文件时的转换

    INI文件中的值都被视为字符串,如果需要将值解析为其他类型(如浮点数或数组),需要在代码中进行转换:

    data = ini2struct('test.ini');
    
    % 从结构体中获取对应的值
    robotValue = data.Robot;
    limitAValue = str2num(data.Limit.A); % 将字符串转换为数值
    limitBValue = str2num(data.Limit.B); % 将字符串转换为数值
    
    % 打印结果
    disp(['Robot: ' robotValue]);
    disp(['Limit A: ' num2str(limitAValue)]);
    disp(['Limit B: ' num2str(limitBValue)]);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
  • 相关阅读:
    数据结构之单链表
    数据结构——排序算法——堆排序
    【免费Web系列】大家好 ,今天是Web课程的第十八天点赞收藏关注,持续更新作品 !
    测试进阶必备,这5款http接口自动化测试工具不要太香~
    如何理解UML2.5.1(02篇)
    IDEA中报错:因为在此系统上禁止运行脚本有关详细信息,请参阅...(图文解释 亲测已解决)
    7-91 统计字符出现次数
    洁净室悬浮粒子:手持式、在线式、便携式粒子计数器如何选择呢?
    Lecture 3 Process Concept(进程概念)
    vue2中怎么使用css样式实现给元素穿衣的功能
  • 原文地址:https://blog.csdn.net/weixin_43455581/article/details/132732805
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号